Why is it a bad thing to have multiple HTML elements with the same id attribute?

Based on your question you already know what w3c has to say about this:

The id attribute specifies a unique id for an HTML element (the id
attribute value must be unique within the HTML document).

The id attribute can be used to point to a style in a style sheet.

The id attribute can also be used by a JavaScript (via the HTML DOM)
to make changes to the HTML element with the specific id.

The point with an id is that it must be unique. It is used to identify an element (or an anything: if two students had the same student id schools would come apart at the seems). It’s not like a human name, which needn’t be unique. If two elements in an array had the same index, or if two different real numbers were equal… the universe would just fall apart. It’s part of the definition of identity.

You should probably use class for what you are trying to do, I think (ps: what are you trying to do?).

Hope this helps!

Leave a Comment