Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 617
penpen Avatar asked Nov 02 '11 21:11

penpen


People also ask

Can an element ID be a number?

Rules for Using the ID Attribute The ID must start with a letter (a-z or A-Z). All subsequent characters can be letters, numbers (0-9), hyphens (-), underscores (_), colons (:), and periods (.). Each ID must be unique within the document.

Can we give a numeric value for ID of an HTML element?

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 ("."). Show activity on this post. Because that's what the HTML4 spec dictates.

Can you use numbers in ID JavaScript?

There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.

Can a CSS ID have a number?

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.


2 Answers

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.

like image 94
LukeH Avatar answered Sep 27 '22 17:09

LukeH


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.

like image 45
Matt Ball Avatar answered Sep 27 '22 18:09

Matt Ball