views:

48

answers:

4

I am using MyTableGrid to show an Excel like control in my webpage.

The cells are referenced with ids like "mtgIC1_0,2" for table 1, column 0, row 2.

Unfortunately, when I try to use the jquery selector with this id $("#mtgIC1_0,2"), it never works.

I figured it is because of the "," since it works for any other ids in the page without coma.

+4  A: 

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 (".")

However if you escape the comma it should still work

e.g

$('#mtgIC1_0\\,2')
redsquare
Beat me to it and good advice on escaping.
TNi
Note that the first part of this answer is specific to is for HTML4 and below, so this won't be valid later on :)
Nick Craver
Nick, if we took that approach nothing would be correct!
redsquare
Blarg, I was escaping with a single \ =(
Eric
@redsquare - That's a legit concern...but pages that are HTML5 are a current reality, not something 5 years from now, so it's something to keep in mind for current developers finding this as well.
Nick Craver
@NickCraver not really a concern of mine, I was 'trying' to be sarcastic:)
redsquare
+1  A: 

From here http://api.jquery.com/category/selectors/

"If you wish to use any of the meta-characters (#;&,.+*~':"!^$[]()=>|/ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an input with name="names[]", you can use the selector $("input[name=names\\[\\]]")."

Nikita Rybak
Although correct to escape special characters... As @redsquare notes - ID identifiers can not legally contain commas.
scunliffe
+1  A: 

I don't know offhand if this is the reason, but according to here, ID names should not contain commas. The relevant sentence is:

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 (".").

TNi
thanks, but "should not happen" and "stuck to deal with it" are two different worlds =)
Eric
+1  A: 

If you have known coordinates of the table, you can target the cell like this:

$('#myTable tr:nth-child(2) td:nth-child(2)').css('background-color', '#F00');
AndrewDotHay
+1 for an innovative answer
redsquare