Calling JQuery from Javascript function

Posted by Virendradugar under jQuery category on | Views : 22541
In below example, I have placed a button. On button client click, I have changed the CSS of button using JQuery. I have also changed the color of button on page load.See code.

<script type="text/javascript" src="Script/jquery-1.3.2.js"></script>
<script type="text/javascript" language="javascript">
function Test()
return false;

Here is the button

  <asp:Button ID="btnName" runat="server" Text="Change CSS" OnClientClick="return Test();" />

In the Javascript function Test, you can directly write $("#btnName").css("color","red");. Instead of putting this in document.ready function. It will work in case of a small page but when the page is heavy and it's not completely loaded and you click on button, then it's quite possible that you can get an error of undefined object. And it's a best practice to always put your jQuery code in $(document).ready() function.

$(document).ready() gives the assurance that DOM is loaded.

Visit my blog


Comments or Responses

Login to post response