Friday, September 18, 2009

Issue with "$(document).ready(function() {...}" in JQuery

Today one of my friend faced a peculiar issue with JQuery. He told me, JQuery is working fine for ASP.NET controls if he add the JQuery code inline. But it is not working, if he move the code to an .js file and use it in the page.
Then we thought something wrong with the intelligent selector in JQuery. But the syntax of the intelligent looked perfect and no issues were there.
$("input[id$='txtName']").val("Text Added using jQuery");

Then I asked him, are you sure you have only one "$(document).ready(function() {...}" in the ASP.NET page. That ring a bell and when he checked, it was present in ASP.NET page and in .js file. Once he remvoed the "$(document).ready(function() {...}" from one place, Bingo!!! it started working.

Hence, an ASP.NET page can contain only one "$(document).ready(function() {...}". We need to be sure that, in the referenced .js files and in the inline javascript code it should not be present more than once.

No comments:

Post a Comment