views:

39

answers:

1

Hi all:

Has anyone tried matching an id with an equal sign (=) in it, and WORKED? E.g.:

// DOM structure
/*DOC += <span id='Test=Test' class='something'></span> */
var test = $('#Test=Test');

I tried the above code. jQuery doesn't like it and returned undefined. However if I match the span's class, I could actually find the span (and subsequently query the value of id).

If I have to match the id with '=' in it, are there any ways to do it?

Thanks.

+4  A: 

Well, at first I want to notice that the equal sign is not a valid character for the ID attribute, it can still be used, but you might have unexpected behavior between different browsers.

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

About jQuery, the = character on selectors must be escaped by a double back-slash:

$('#Test\\=Test');

More info:

The full list of characters that need to be escaped: #;&,.+*~':"!^$[]()=>|/

CMS
As you say, the equal sign is not valid in an ID attribute, so why are you saying it can still be used?
Tim Down
Cause you can. But should not.You can use it at your own risk.
NilColor
If you use an ID with an equal sign, your document will not be valid HTML, meaning all bets are off as to whether a user agent will even render it. Even if it does (as all the major browsers will), all ID-related behaviour (e.g. CSS selectors, document.getElementById, URL hashes) will have undefined behaviour (and here I would bet there will be variations in browser behaviour). Why even consider risking it?
Tim Down