107

From what I understand the HTML5 spec lets you use IDs that are numbers like this.

<div id="1"></div>
<div id="2"></div>

I can access these fine using getElementById but not with querySelector. If I try do the following I get SyntaxError: DOM Exception 12 in the console.

document.querySelector("#1")

I'm just curious why using numbers as IDs does not work querySelector when the HTML5 spec says these are valid. I tried multiple browsers.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Berry Blue
  • 13,060
  • 16
  • 51
  • 91

5 Answers5

119

It is valid, but requires some special handling. From here: http://mathiasbynens.be/notes/css-escapes

Leading digits

If the first character of an identifier is numeric, you’ll need to escape it based on its Unicode code point. For example, the code point for the character 1 is U+0031, so you would escape it as \000031 or \31 .

Basically, to escape any numeric character, just prefix it with \3 and append a space character ( ). Yay Unicode!

So your code would end up as (CSS first, JS second):

#\31  {
    background: hotpink;
}

document.getElementById('1');
document.querySelector('#\\31 ');
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Dennis
  • 30,518
  • 9
  • 59
  • 75
  • 1
    What would the syntax be for values greater than 9? I can't get this to work with IDs like 10. – Berry Blue Dec 03 '13 at 01:23
  • 5
    You need a space after the first character: `#\\31 0` - you can refer to http://mothereffingcssescapes.com/ – Dennis Dec 03 '13 at 23:41
  • Thank you for the follow-up and the link! – Berry Blue Dec 04 '13 at 14:17
  • 1
    Note that the space is only necessary if a character that is a hex digit immediately follows the escape sequence, in order to distinguish that character from the escape sequence. See http://www.w3.org/TR/CSS21/syndata.html#characters for more details. – BoltClock May 18 '14 at 13:46
96

Because while they are valid in the HTML5 spec, they are not valid in CSS, which is what "query selector" means.

Instead, you would have to do this: document.querySelector("[id='1']"), which is very long-winded considering you could give it a meaningful ID like message1 or something ;)

Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
30

I needed an approach which was automated. A recent change meant the id values used were no longer simple alphabetic characters and included numbers and special characters.

I ended up using CSS.escape: https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape

console.log(CSS.escape('1'));

First, this is the failing case:

const theId = "1";
document.querySelector(`#${theId}`);
const el = document.querySelector(`#${theId}`);
el.innerHTML = "After";
<div id="1">Before</div>

And now using CSS.escape:

const theId = "1";
const el = document.querySelector(`#${CSS.escape(theId)}`);
el.innerHTML = "After";
<div id="1">Before</div>

See how it correctly changes to show After, demonstrating the selector worked!

Glavin001
  • 787
  • 2
  • 13
  • 28
3

From the W3C documentation Attribute selectors syntax

Attribute values must be a valid CSS identifiers or String.

Thus, digits or alphanumeric strings with leading digit does not qualify as a valid identifier.

If you are using an ID generator utility for generating an identifier, you might end up with alpha numeric ids with leading digits.

A quick fix would be to either omit digits from the SEED of the generator( if it can be modified ) or always append a string to the id generated.

3

Here is a function I made just now for dealing with leading number ID's in CSS selectors, and it is IE safe as CSS.escape is not.

Pass the selector through this cleanSelector function before using it :

var cleanSelector = function(selector){
    (selector.match(/(#[0-9][^\s:,]*)/g) || []).forEach(function(n){
        selector = selector.replace(n, '[id="' + n.replace("#", "") + '"]');
    });
    return selector;
};

var myselector = ".dog #980sada_as div span#aside:hover div.apple#05crab:nth-of-type(2), .ginger #2_green_div, div.cupcake #darwin p#23434-346365-53453";

var clean_myselector = cleanSelector(myselector);

// print to show difference
console.log(myselector);
console.log(clean_myselector);

//use the new selector like normal
var elems = document.querySelectorAll( clean_myselector ); 


Per
  • 61
  • 2