Kapil Verma
Kapil Verma

Reputation: 178

How to define jquery selector valid id?

I am using following jquery selector by id

$("#rs_0.01").val();

So my small question is: this valid or not ? As it's not working in my case.

Upvotes: 1

Views: 351

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115242

Check documentation of jQuery Selectors

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"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.


So you can use

$("#rs_0\\.01").val();

or use attribute equals selector instead

$("[id='rs_0.01']").val();

Upvotes: 4

Lokesh_Ram
Lokesh_Ram

Reputation: 391

In your case you got few ways to retrieve value of a textbox by using id selector with the same name which you have specified in the question..

One way is by doing like this:

$("[id='rs_0.01']").val();

or you can use the escape sequence inbetween the selector name like this:

$("#rs_0\\.01").val();

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

Use: You need to escape special chars:

$("#rs_0\\.01").val();

Upvotes: 0

Related Questions