Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector with ids and comma

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.

like image 874
Eric Avatar asked Jul 08 '10 23:07

Eric


People also ask

How to select element with class and ID in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How to select element with ID and class?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

How can use multiple ID selectors in jQuery?

Approach: Select the ID's of different element and then use each() method to apply the CSS property on all selected ID's element. Then use css() method to set the background color to pink to all selected elements. Display the text which indicates the multiple ID selectors.


2 Answers

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')
like image 174
redsquare Avatar answered Oct 13 '22 00:10

redsquare


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\\[\\]]")."

like image 29
Nikita Rybak Avatar answered Oct 13 '22 00:10

Nikita Rybak