Tuesday 2 July 2013

jQuery Selectors NOT working...

OOPS!!!! jQuery Selectors NOT working.. A very common jQuery problem/issue that you must have face or may face. And there could be tons of reason of selectors not working properly like incorrect syntax, incorrect context.. But do you know there could be another reason which is element ID or css class name? 

Yes, you read it right. Sometimes, if the ID or class containing meta characters such as @ # !" $ % &' ()*+,./:;<=>?[\]^`{|}~ like "foo.bar", "xyz@pqr" , "id#title"....

For example, if your element ID is "myid.3" then below code will not work.
1$('#myid.3').text('DIV Content Updated');
From jquery API document :

"To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar")"


So, the correct syntax is,
1$('#myid\\.3').text('DIV Content Updated');
So, if your ID contains any meta character then use backslashes for selecting elements. But it is not advisable to use meta-characters in DOM identifier as your HTML is not validated.

Reference : http://jquerybyexample.blogspot.com/


Good Luck...

No comments:

Post a Comment