0

I want to do some jquery fonction on onclick event on one link with id "1_#FF0000".

When i use jquery id selector like

$("#1_#FF0000")

it is not selecting my link

can any one suggest me how to use jquery selector in this type of case

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
Jeet
  • 1,350
  • 1
  • 15
  • 32
  • your id is starting with number .. ? – Richa Dec 23 '13 at 11:57
  • 3
    you shouldn't start ID with number even it is HTML5 valid – A. Wolff Dec 23 '13 at 11:57
  • its better to use class in your context – patel.milanb Dec 23 '13 at 11:58
  • *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 (".").* From the [w3 specification](http://www.w3.org/TR/html4/types.html) – h2ooooooo Dec 23 '13 at 11:58
  • @h2ooooooo this is no more the case in HTML5, but still in CSS and just better to not use a number at the beginning of an attribute ID – A. Wolff Dec 23 '13 at 11:59
  • ..and simply to add a manual entry for what @A.Wolff mentioned: [specification for ID tag in HTML5](http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute). Here's a [thread](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) discussing this. – h2ooooooo Dec 23 '13 at 12:00
  • @patel.milanb - Why a class? OP wants to identify a specific element, which is exactly what the id attribute is for. – nnnnnn Dec 23 '13 at 12:02
  • 1
    @nnnnnn: agree. BUT using ID(starting with number, includes #) why not give it a specific class and use it.. and stay up-to-date with HTML specification – patel.milanb Dec 23 '13 at 12:04
  • 1
    @patel.milanb I'm agree, i think OP would better to use classes instead of IDs because i guess he has more than one element for red (#FF0000) and other color – A. Wolff Dec 23 '13 at 12:15
  • @A.Wolff: couldnt agree more.. i also think he has list of colors on his page.. – patel.milanb Dec 23 '13 at 12:28

4 Answers4

7

you need to escape it using \#

$("#1_\\#FF0000")
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
5

Try this way

$("[id^=1_#FF0000]")

OR

 $("[id='1_#FF0000']")

DEMO

Reference

http://api.jquery.com/category/selectors/attribute-selectors/

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
3

You can also do this:

$("[id='1_#FF0000']")

attribute-equals-selector

Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
0

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

HTML Specification

codingrose
  • 15,563
  • 11
  • 39
  • 58