Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

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

My question is, 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.

2 Answers

0 votes
by (40.7k points)

If the element has multiple ID attributes, all of them must be treated as IDs for that element for the purposes of the ID selector. It can be done by using mixtures of xml:id, DOM3 Core, XML DTDs, and namespace-specific knowledge.

Note: An XHTML element can have multiple ids, for example: <p id="foo" xml:id="bar"> But for assigning multiple ids to the same id attribute using a space-separated list is not possible.

0 votes
ago by (1.7k points)

No, an HTML element cannot have multiple IDs. According to the recommendation, the `id` attribute must contain a unique identifier within the document; the following code is illegal, and the behavior on browsers will be unpredictable when an element has multiple `id`s: `id=\\\"nested_element_123 task_123\\\"`.

Attach a class attribute whenever you are linking two or more identifiers to one element, which is alright because classes can hold many values-for example, in the above example `class="nested_element_123 task_123"`.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 9, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...