Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an HTML element have multiple ids?

I understand that an id must be unique within an HTML/XHTML page.

For a given element, can I assign multiple ids to it?

<div id="nested_element_123 task_123"></div> 

I realize I have an easy solution with simply using a class. I'm just curious about using ids in this manner.

like image 547
webmat Avatar asked Oct 10 '08 16:10

webmat


2 Answers

No. From the XHTML 1.0 Spec

In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above. See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html.

like image 185
timmow Avatar answered Sep 19 '22 19:09

timmow


Contrary to what everyone else said, the correct answer is YES

The Selectors spec is very clear about this:

If an element has multiple ID attributes, all of them must be treated as IDs for that element for the purposes of the ID selector.Such a situation could be reached using mixtures of xml:id, DOM3 Core, XML DTDs, and namespace-specific knowledge.


Edit

Just to clarify: Yes, an XHTML element can have multiple ids, e.g.

<p id="foo" xml:id="bar"> 

but assigning multiple ids to the same id attribute using a space-separated list is not possible.

like image 43
user123444555621 Avatar answered Sep 18 '22 19:09

user123444555621