How to take id as selector when used hash?

To use any of the meta-characters such as !”#$%&'()*+,./:;<=>?@[]^`{|}~ as a literal part of a name, it must be escaped with with backslashes:

css way:

#test\#1{
    color: red;
}

jquery way:

$('#test\\#2').css('color','blue');

demo


Recommendation:

You should not use # in your id see more for detail

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 (“.”).


So, you may be also wondering id=”test.1″ can be used and for this too you should escape the character as described above.

Leave a Comment