Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which DOM elements cannot accept an id?

Tags:

html

dom

I'm reading this basic tutorial on canvas elements. The (almost) in the following sentence caught my eye:

The id attribute isn't specific to the element but is one of the default HTML attributes which can be applied to (almost) every HTML element

Which html elements cannot accept an id?

like image 989
Randomblue Avatar asked Jun 19 '12 18:06

Randomblue


People also ask

Can two DOM elements have same ID?

The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.

Can ID have special characters?

Note: Technically, the value for an id attribute may contain any character, except whitespace characters. However, to avoid inadvertent errors, only ASCII letters, digits, '_' , and '-' should be used and the value for an id attribute should start with a letter.

Can HTML ID contain special characters?

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

Can elements share ID?

Applying the same id to multiple elements is invalid HTML and should be avoided. For that purpose we have class attributes, which may be distrbuted everywhere and anywhere. The reason we can only have one id is because it is a fragment identifier that can be hooked with an HREF.


2 Answers

From w3schools (yes, I know...) :

Note: The id attribute is not valid in: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

Note that this is only valid for HTML4 but that explains the "almost" of the tutorial.

As others have pointed out, HTML5 accepts id on all elements.

like image 148
Denys Séguret Avatar answered Oct 23 '22 20:10

Denys Séguret


In HTML5, the id attribute is a global attribute and can be specified on any element.


If you look through the Document Type Declaration for HTML4, you can find the elements which do not have %attrs; defined in their attribute list to indicate they do not support the id attribute. Those included are near the bottom in the "Document Head" section: HEAD, TITLE, BASE, META, STYLE, SCRIPT, and HTML.

Note that although the PARAM element does not include the %attrs; declaration in its attribute list, it does explicitly allow the id attribute itself in that list.

<!ATTLIST PARAM
  id          ID             #IMPLIED  -- document-wide unique id --
  name        CDATA          #REQUIRED -- property name --
  value       CDATA          #IMPLIED  -- property value --
  valuetype   (DATA|REF|OBJECT) DATA   -- How to interpret value --
  type        %ContentType;  #IMPLIED  -- content type for value
                                      when valuetype=ref --
  >
like image 39
animuson Avatar answered Oct 23 '22 22:10

animuson