tags:

views:

53

answers:

4

Hw can i select component with id, which contain [ ] symbols?

for example:

<div id=divtables[COURSE_SUBJECTS][141][TITLE]>gg</div>

Tnx.

+1  A: 

[] symbols aren't valid id characters. If you want to try it out, use a double backslash:

$("#divtables\\[COURSE_SUBJECTS\\]")
kgiannakakis
Exan
A: 

You just need to escape them, like this:

$("#divtables\\[COURSE_SUBJECTS\\]\\[141\\]\\[TITLE\\]")

There's a full FAQ/how-to on the escaping topic here, this works for any selector type, though you should have different IDs values possible (ones that don't result in invalid HTML mainly). I know this isn't always the case, e.g. content generated by another system, the solution above works for this, but it's not the best solution, replacing any invalid characters would be best, I'd suggest this:

<div id="divtables-COURSE_SUBJECTS-141-TITLE">gg</div>

Then your selector doesn't need any tricks, it's just:

$("#divtables-COURSE_SUBJECTS-141-TITLE")
Nick Craver
+2  A: 

This is not a valid id according to W3C:

Naming rules:

Must begin with a letter A-Z or a-z

Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

Values are case-sensitive

This basically says that you should not have [] in an id. I would strongly suggest you following the standards.

Darin Dimitrov
Does HTML5 still have this restriction? I'm unable to find it, spec doesn't mention it like it does in HTML4: http://www.w3.org/TR/html5/dom.html#the-id-attribute
Nick Craver
+2  A: 

As per w3c standards you cannot use [] for an ID property in HTML. That means you can only use the following characters for ID property.

 Letters ([A-Za-z])
 digits ([0-9])
 hyphens ("-")
 underscores ("_")
 colons (":")
 periods (".").
Joe
It should be noted this is for HTML4, I don't think HTML5 has such a restriction: http://www.w3.org/TR/html5/dom.html#the-id-attribute
Nick Craver
@Nick Craver Yes, You are correct. There is no restrictions in html5 except it cant be null.
Joe