I want to refer to this element (using jQuery, if possi...">

JQuery: escaping plus sign in selector

For the dom element:

<li id="foo+bar"></li>

I want to refer to this element (using jQuery, if possible), and I'm trying:

$("#foo+bar")

and I also tried to avoid it too:

$("#foo\+bar")

.. but none of them work, because (I guess) jQuery cannot handle the plus sign that seems. Unfortunately, I have no way to change the id value, therefore:

Is there a way to get jQuery to get the correct dom, or how can I approach this problem?

+3
source share
3 answers

Try executing it with two backslashes.

$("#foo\\+bar")
+9
source

Well, the problem is probably what +should not be.

The HTML 4 specification says:

ID NAME ([A-Za-z]), , ([0-9]), ( "-" ), ( "_" ), ( ":" ) ( "." ).

, - , . .

+6

I do not believe that + is a valid character in id.

Identifier identifiers and NAME must begin with a letter ([A-Za-z]) and can be performed with any number of letters, numbers ([0-9]), hyphen ("-"), underscore ("_"), colons ( ":") and periods ("").

http://www.w3.org/TR/html401/types.html#type-name

+1
source

All Articles