28

Working on a project, nearly finished and just tidying up the HTML and I find out that you're not really allowed to have an ID that is just a number-

< a> attribute "id" has invalid value "567" The attribute ID is of type ID. As described above, it should begin with a letter and have no spaces

Good    <a id="567" href="/index.html">
Good    <a id="n567" href="/index.html">

I can go through my code and add a letter then strip it when the value is used in my jQuery but it would be messing around I don't really need.

Is there a reason I shouldn't be using numbers as ID's?

penpen
  • 935
  • 3
  • 12
  • 22

2 Answers2

42

That's just what the spec says.

From the HTML 4 specification:

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

The good news is that the HTML 5 specification is more lenient:

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

duncan
  • 31,401
  • 13
  • 78
  • 99
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 2
    First time HTML5 has come in handy for me! Still no real reason why HTML4 doesn't like it but I'm glad I'm not doing anything wrong. – penpen Nov 02 '11 at 22:16
  • 9
    I used numeric IDs since 2002 and no browser ever failed to work with them properly. I think such spec is as good as useless and can be safely disregarded. – andreszs Aug 14 '14 at 22:08
15

Why can't I have a numeric value as the ID of an element?

Because that's what the HTML4 spec dictates.


On the other hand, the HTML5 spec has removed this requirement.

duncan
  • 31,401
  • 13
  • 78
  • 99
Matt Ball
  • 354,903
  • 100
  • 647
  • 710