Reputation: 4307
I'm using jQuery 1.7.1, and no other JS library/framework. I can select most elements just fine, except for one particular table that is giving me trouble. This happens in both Firefox and Chrome.
Here is an image that describes it all:
The steps in this image, in order:
Why won't jQuery just hand over the table to me? Why might this happen?
Upvotes: 2
Views: 203
Reputation: 260
You shouldn't use any of these meta-characters: !"#$%&'()*+,./:;<=>?@[\]^{|}~
in your ids, classes tagnames, attributes and values(also the backtick: ` , but i cant put it in a code block ^^).
if you have to use these characters, you can escape them in your selector with two backslashes
$('#office_set-\\#\\#\\#')
btw your question is really relevant when these characters are used as javscript code e.g. in a onclick attribute. You can find elements containing a specific code only when you properly escape the meta-characters (of which there will be a lot).
Source and more information here: http://api.jquery.com/category/selectors/
Upvotes: 2
Reputation: 17487
You need to quote the # characters in your ID, since that character is a special character to jQuery.
$('#office_set-\\#\\#\\#');
It's double backslashes so JavaScript converts them to single backslashes which jQuery then sees.
Upvotes: 0
Reputation: 1
1) Make sure that the selector of table on step 5 is right. Maybe there's more than one element with the same id.
2) When you assign a JQuery element to a variable you can't call the raw DOM attributes just like that. Try:
theTableIWant.attr('id');
Or:
var theTable = $(theTableIWant).get(0);
theTable.id;
Upvotes: 0
Reputation: 35793
It doesn't work because #
is a special character in jQuery selectors for ID's so you get this error: Syntax error, unrecognized expression: #office_set-###
If you really have to use ###
in the ID (I wouldn't recommend it for the reason Matt posted) you can escape it in the select like this:
$('#office_set-\\#\\#\\#')
Example - http://jsfiddle.net/infernalbadger/fpcME/
You have to double escape as you actually want to pass the \
to the jQuery selector engine.
Upvotes: 3
Reputation: 7134
"#" is not officially valid within DOM id's: http://www.w3.org/TR/REC-xml/#NT-Name and therefore you won't get consistent support.
Upvotes: 2