Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the side effects of using multiple instances of a single id name in HTML?

Tags:

html

Just curious to find out exactly what problems I'll cause myself if I include multiple elements in my web-page with the same id?

for example:

<div id='someID'>Some Content</div>
<div id='someID'>Some Other Content</div>
like image 376
David Meister Avatar asked Mar 21 '10 12:03

David Meister


People also ask

What happens if multiple HTML elements have the same ID?

The id must be unique. There can be only one element in the document with the given id . If there are multiple elements with the same id , then the behavior of methods that use it is unpredictable, e.g. document. getElementById may return any of such elements at random.

Can ID be used multiple times in HTML?

HTML id Attribute. 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.

How many times can you use ID in HTML?

You can have as many ids as you want on a page but you can only use a specific id once. For example; id="logo" can only be used once on a page. You could also use id="gallery" on the same page but it can be used only once on that page. On the other hand class="someClass" could be used multiple times on a page.

Can ID selector be used more than once?

You can use ids more than once. The browser will still apply the styles. It is, however, not best practice to do so and is classed as invalid markup. To get around it you can add a class to the element.


5 Answers

When you try and reference those elements from JavaScript it won't be able to resolve which element you are referring to. Depending on the specific JavaScript interpreter you are running on, you may either get errors, or undefined behaviour - both cases are undesirable.

The HTML spec states that Id's should be unique, so your HTML will be considered as invalid and may cause browsers to drop back into quirks mode rendering, or even totally refuse to render your page (although that's not likely, and all current browsers will render something - technically, if you don't follow the spec the browser is under no obligation to do anything but reject your page).

You should consider using class names instead if you want something to identify multiple elements by:

<div class="someClass">Some Content</div>
<div class="someClass">Some Other Content</div>

Blair has pointed out in the comments below that if you need to search for elements by class from JavaScript, you can speed the search up by going from the nearest element with an ID and also tell it what node type to look for. This can keep the access speed fast in a lot of cases, but doesn't break any rules on duplicate id's:

HTML:
<div id="myNearestID">
    <div class="someClass">Some Content</div>
    <div class="someClass">Some Other Content</div>
</div>

JavaScript+JQuery:
$('#myNearestID div.someClass')
like image 67
Simon P Stevens Avatar answered Nov 16 '22 12:11

Simon P Stevens


IE will have major problems with any javascript using IDs, that's the main side-effect.

Overall, IDs are unique, per the spec...the browser maker is free to make this assumption in their code, and any bugs (Javascript, CSS, etc) that result from invalid HTML involving them...well, that's your problem :)

The browser isn't obligated to fix it really, and I don't think they should either.

like image 24
Nick Craver Avatar answered Nov 16 '22 11:11

Nick Craver


I’m not sure how browsers render the CSS rules of type #someID in such case (they will probably apply the rule to all such elements), but you’ll certainly run in trouble with JavaScript and getElementById.

like image 34
zoul Avatar answered Nov 16 '22 13:11

zoul


I read elsewhere that IE creates a global variable for each element with an ID.

Am I wrong in saying that's an issue too?

like image 21
David Meister Avatar answered Nov 16 '22 13:11

David Meister


A couple more points not already noted.

Fragment identifiers: How would the browser handle a link to http://www.example.com#someID ?

Internal references : If someID was on two input elements, and you had <label for="someID">... if the user clicked on the label, the browser would have to decide which input should receive focus. (In HTML5, this is defined to be the first input, which is not necessarily what you want). Likewise for ARIA support, some attributes such as aria-labelledby and aria-describedby rely on pointing to a single place.

like image 1
Alohci Avatar answered Nov 16 '22 13:11

Alohci