3

Given a dom element:

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

I want to address that element (using jQuery if possible) and I try:

$("#foo+bar")

and I've also tried also escaping it:

$("#foo\+bar")

..but neither seems to work, because (I'm guessing) jQuery can't handle the plus sign it seems. Unfortunately I don't really have the option of changing the id value, so:

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

mikkelbreum
  • 3,021
  • 9
  • 38
  • 41
  • 3
    Did you forget to put the `#` before the id name? – namuol May 24 '11 at 19:17
  • See also http://stackoverflow.com/questions/739695/jquery-selector-value-escaping – kennytm May 24 '11 at 19:18
  • C'mon, folks, it's **[in the jQuery FAQ](http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_element_by_an_ID_that_has_characters_used_in_CSS_notation.3F).** – Matt Ball May 24 '11 at 19:19

3 Answers3

11

Try escaping it with two backslashes.

$("#foo\\+bar")
Ole Melhus
  • 1,899
  • 12
  • 17
  • works fine, thank you.. and yes Matt Ball, it's in the jQuery FAQ I see it, sorry I didn't find it there first. – mikkelbreum May 24 '11 at 19:30
  • Keep in mind although that the **escape** code is in the FAQ, it isn't specifically pointing to your `+`, but it is (kinda explicitly too!) reffering to actual valid ID-problems like the `:` and the `.`. – Nanne May 25 '11 at 07:45
6

Well, the problem is probably that a + shouldn't be there.

HTML 4 specification says:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

So any workaround is still based on a faulty start, so might stop working anytime. You really should change that id.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 1
    It's fine in HTML5. For a jQuery selector, http://stackoverflow.com/questions/6115636/jquery-escaping-plus-sign-in-selector/6115667#6115667 – Matt Ball May 24 '11 at 19:18
  • It might be, so in an HTML5 only situation that would be cool. As HTML5, though wildly used for fun, is not yet a finished spec as far as I know, I would advice against coding for that. – Nanne May 24 '11 at 19:20
  • But my point is that any solution is fighting against something that shouldn't be there: experience tells me that that will mean that somewhere in the future your code will randomly break because something (jquery, browser, whatever) has stopped supporting your workaround. Anything you do (have not tried it, the double escape by @ole_melhus probably works) is dangerous ground as far as i'm concerned. – Nanne May 24 '11 at 19:27
1

I don't believe + is a valid character in an id.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

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

James Montagne
  • 77,516
  • 14
  • 110
  • 130