jQuery tip: Selecting ids with periods in them (dots/'.')

I was messing around with jQuery today combined with a Spring form-backed bean and was finding that my jQuery was failing to work. The id of the input field bound to my Spring form was person.id.

Trying to get the value entered into the input box was failing, this is what I was trying:

$('#person.id').val();
The problem here is the period - '.' - jQuery see these as CSS notation and so fails to find an element with that id, what you need to do is add two backslashes ('\\') before the period to escape it, like this:
$('#person\\.id').val();
See the jQuery FAQ for more details.

Technorati Tags: , ,

No comments: