What characters can I use in an identifier for an HTML element?
For example:
<SPAN id="section[5]" ...>
(Or rather, should I stick to certain characters to make sure the id
works across all major browsers/JavaScript engine).
What characters can I use in an identifier for an HTML element?
For example:
<SPAN id="section[5]" ...>
(Or rather, should I stick to certain characters to make sure the id
works across all major browsers/JavaScript engine).
In HTML5, the only restrictions are that the ID must be unique within the document, contain at least one character and contain no spaces. See http://www.w3.org/TR/2014/REC-html5-20141028/dom.html#the-id-attribute
As other answers have pointed out, HTML 4 is more restrictive and specifies that
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 (".").
In HTML 4, the id
attribute holds a NAME token which are defined:
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 (".").
As pointed out in a previous post, depending on your implementation you may have troubles with colons and periods along with others: What are valid values for the id attribute in HTML?
From http://www.w3.org/TR/html4/types.html:
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 HTML4 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 (".").
actually I can successfully use a ° too for example
< span id= "test°" />
Seems to be allowed, and I have no problem with jQuery or other ways to fetch dom elements via the selectors. Perhaps other chars work, but I didn't test them all.