Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a HTML element have multiple unique ID attributes? [duplicate]

Tags:

Needed to know if an HTML element can have multiple attributes of ID's, for instance :

<input type="text" id="identifier1" id="selector1" />

As I needed to clarify this statement mentioned about selectors at W3 website.

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.

The Possible duplicates which people are referring, states question for this syntax

<input type="text" id="identifier1 selector1" />

which is different than syntax that I am asking.

like image 637
Abhishek Madhani Avatar asked Jun 05 '13 09:06

Abhishek Madhani


People also ask

Can there be multiple ID attributes in HTML?

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 you have duplicate ids in HTML?

Each ID in your HTML document must be unique. Using the same ID on more than one element may cause screen readers and other assistive technologies to only announce the first element with the shared ID, preventing users from accessing the later elements.

Can multiple HTML elements have the same ID What About the same class?

This is because an id value can be given to only one HTML element, in other words, multiple elements cannot have the same id value on the same page. For example, you can have only one element with a #header id or one element with a #footer id on your page.

Can an attribute be applied multiple times to the same element?

It is not valid to have the same attribute name twice in an element.


2 Answers

Needed to know if an HTML element can have multiple attributes of ID's

Short answer? No because the browser will only render the first one.

See this Fiddle, I can only target it in CSS using the first id that appears in the DOM. Try changing that CSS selector to use the second id, it won't work.

That's because it seems like the second ID is being disregarded by the browser, as this is the output HTML:

<input type="text" id="identifier1">

If you really need additional identifiers on an element, you should think about using either multiple class names or data attributes to correspond to additional data.

like image 74
mattytommo Avatar answered Oct 28 '22 11:10

mattytommo


Needed to know if an HTML element can have multiple attributes of ID's

No. No element in HTML may have more then one instance of a given attribute.

As I needed to clarify this statement

Note the last sentence in that statement.

Also note that the CSS idea of an "ID attribute" is not "An attribute with the name id". Also quoting from that document:

Document languages may contain attributes that are declared to be of type ID

Only the id attribute is an ID type in HTML.

like image 32
Quentin Avatar answered Oct 28 '22 12:10

Quentin