tags:

views:

1288

answers:

2

If my html looked like this:

<td class="controlCell">
 <input class="inputText" id="SearchBag.CompanyName" name="SearchBag.CompanyName" type="text" value="" />
</td>

How could I select #SearchBag.CompanyName with JQuery? I can't get it to work and I fear it's the dot that's breaking it all. The annoying thing is that renaming all my id's would be a lot of work, not to mention the loss in readability.

Note:
Please let's not start talking about how tables are not made for lay-outing. I'm verry aware of the value and shortcommings of CSS and try hard to use it as much as possible.

+4  A: 

One variant would be this:

$("input[id='SearchBag.CompanyName']")
Tomalak
Indeed. Could you tell me why your solution works and $("#SearchBag.CompanyName") doesn't?
borisCallens
Because .CompanyName is treated as a class selector.
cletus
Quickly looking at the source code, it is either a deliberate design decision or a bug. The regex used to identify id selectors (named quickExpr) does not include the dot as a valid character. The HTML spec however allows it.
Tomalak
Then howcome your selector does include the dot and mine doesn't?
borisCallens
@cletus: But since ID selectors must be preceded by a hash #, there should be no ambiguity here. HTML allows a dot in the ID attribute, so should CSS/jQuery. Can you point out a situation where allowing this could cause trouble? (I can't, but I'm not sure.)
Tomalak
Having a class selector on an ID is possibly useful. Imagine you have a #menu element on every page but want to make it different on one of your pages. #menu.hideme for example? Marginal I know but it's valid.
cletus
Hm... I guess that's a valid point, though I never thought about it like that. I would always go with a ".hideme" class and attach that to whatever I want hidden. I don't see much difference between a hidden "#menu" and a hidden "div".
Tomalak
+8  A: 

@Tomalak in comments:

since ID selectors must be preceded by a hash #, there should be no ambiguity here

“#id.class” is a valid selector that requires both an id and a separate class to match; it's valid and not always totally redundant.

The correct way to select a literal ‘.’ in CSS is to escape it: “#id\.moreid”. This used to cause trouble in some older browsers (in particular IE5.x), but all modern desktop browsers support it.

The same method does seem to work in jQuery 1.3.2, though I haven't tested it thoroughly; quickExpr doesn't pick it up, but the more involved selector parser seems to get it right:

$('#SearchBag\\.CompanyName');
bobince
Thanks for the insight. +1 (You should somehow make it obvious that you refer to a comment in my answer)
Tomalak
"#id.class is a valid selector that requires both an id and a separate class to match": As per the HTML spec, IDs must be unique throughout the document. What would "#id.class" be good for? I mean, you can't be any more specific than "#id", can you?
Tomalak
Thanks :) Seems I worked myself in a bit of a mess here though.. :s
borisCallens
@Tom: “#id.class” might be good for multiple documents using the same stylesheet, or setting states with client-side-scripting. For example “#navhome.navselected”.
bobince
@bobince: Yeah, cletus came up with the same example, and I quess it's valid. Still it feels strange to me, and I can't see the genuine advantage of being able to do that. Especially in the light of HTML allowing dots in the ID and the fact that you can always use ".navselected" to achieve the same.
Tomalak
You might, for example, want a selected #navhome to light up in a different colour to a selected #navabout, but other .navselected properties to be shared. I wouldn't say this situation comes up all the time, but I've certainly needed it occasionally.
bobince
It's an edge case, but I can follow the logic (you've got my +1 already). The drawback that remains is that since this isn't caught by quickExpr, the getElementById() shortcut in jQuery is probably not used, and thus it will perform worse even though it is an ID. Same goes for my solution.
Tomalak
Yeah, I would go for ‘_’ rather than ‘.’ if possible; in addition to jQuery performance, the CSS backslash escape is just ugly!
bobince