JQuery selection logic does not work if id matters. in meaning. Any solution?

I am using Spring Forms for my web application. For nested properties, the form tag generates input elements that have an id / name in the form.

For example, Person is a class of commands, and Address is contained in its address field, then the city element will be

<input type="text" id="address**.**city" name="address**.**city" /> 

Now the problem occurs when I try to get its value using jQuery,

 $("#address.city").val(); 

jQuery cannot select any suitable item!

Please let me know any solution.

Thanks in advance.

+6
jquery jquery-selectors
source share
2 answers

Try the following:

 $("#address\\.city").val(); 

From the documentation :

Note. If you want to use any of the metacharacters described above as the literal part of the name, you must escape the character with two backslashes ( \ ). For example:

 #foo\\:bar #foo\\[bar\\] #foo\\.bar 
+17
source share
 $('[id="address.city"]') 

will also work

+6
source share

All Articles