176

Can multiple HTML elements have the same ID if they're of different element types? Is a scenario like this valid? Eg:

div#foo
span#foo
a#foo
omninonsense
  • 6,644
  • 9
  • 45
  • 66
  • 33
    While sometimes possible, it's never valid. – Paul Creasey Apr 10 '11 at 13:16
  • 4
    With all the above being said it is worth to note that it is likely to come across multiple same IDs in a document with user-agent-created content (think frameworks, mv*, react, polymer...). That's if anyone was wondering why a very professional looking XYZ site is full of such *bad practice* coding. – Lukasz Matysiak Aug 21 '17 at 14:34
  • The comment from @PaulCreasey is a good way to answer this problematic question. The question title and body do not match; each of them are reasonable yes or no questions but with different correct answers - this could catch out people who aren't paying attention. There's a meta question about how to resolve question mismatches like this, no answers though as of yet: https://meta.stackoverflow.com/questions/256732 – Richard Abey-Nesbit Aug 14 '20 at 00:52
  • Hi @Tidorith! Thanks for commenting. I'm open to suggestion on changing either the title or the body if you have an idea. The original question was asked out of curiosity. Some codegen tool (I think it might've been some Microsoft UI library) was generating elements with identical IDs. I tried reading the spec and testing it out in browsers, but was left confused since browsers seemed to allow it, while the spec said no. – omninonsense Aug 28 '20 at 12:19
  • @Tidorith Edited the question body a bit. Hope it's better now! – omninonsense Aug 28 '20 at 12:23

17 Answers17

210

No.

Element IDs should be unique within the entire document.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 118
    What are the consequences of not doing so? – corsiKa Dec 09 '12 at 05:11
  • 19
    @corsiKa the consequence is undefined behavior, for example, what does document.getElementById("#foo") or $("#foo") return when there are multiple #foos? You'll run into problems being able to work with these elements from JS, pass them as selectors to libraries/APIs/Flash, etc. – mrooney May 13 '13 at 20:55
  • 1
    Microsoft Outlook builds their webapp like this. It's annoying as hell. How would I reference a specific DIV if there are ones with the same ID? – Jared May 31 '13 at 17:48
  • 24
    why even use multiple similar id's when you got class for this purpose? – Max Yari Oct 14 '14 at 12:22
  • 1
    I think I have found a situation where this seems to occur. If you are using radio buttons on ASP.NET MVC 5 Razor it seems to produce the same ID for each button. I think this may be how the controller deals with input because the id is the same as the ActionResult input parameters. It does not seem to care as long as it gets the right value. I believe there is a way around it but it seems to work OK (but I am not happy with multiple html IDs!). – Cheesus Toast Oct 17 '14 at 17:52
  • 10
    Yes, multiple IDs can, in practice, be replaced by using classes. However, classes are intended for applying styles, not identifying elements, making the scope of names much wider and therefore likely to overlap. Especially if using 3rd party libraries. Id as 'identifier' is not intended to be multiplied so there is clearly a need for something in between. The practical use is componentization of sections of a page/dom into separate logical units. Using (at least) 2-layer identification is hence required. – Alen Siljak Jan 21 '15 at 12:24
  • 2
    Recently had a bug with d3.js and click event handling with multiple, identical IDs. What I observed was the first instance of the ID received the action, even if it was invoked by the second ID. E.g., click on the second instance of id1, and the first instance of id1 would receive the action (change color, text, etc.). – wootscootinboogie Feb 09 '16 at 18:44
  • 2
    Whilst it may be considered bad practice or even invalid HTML 5 by validators, having multiple ID's is certainly possible within the same HTML document. Therefore this answer is not the correct one. – Jeremy Feb 25 '16 at 10:19
  • 1
    A link to the spec: https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute – Koshinae Mar 11 '16 at 09:26
  • So it is completely reasonable to have #content and #myDiv #content for a brief period of time? – Dmytro Aug 05 '16 at 04:39
  • Yes, it's valid. SVG files contain ID names and can even mess up applications that don't account for the possibility of multiple ID names (if you make a G tag with an id, it becomes part of the DOM, so drawing an SVG on screen can introduce multiple id tags, even if you aren't aware it's happening, right on your site. – Nick Steele Sep 08 '16 at 13:29
  • The `:target` selector does only work on ids. Can't use classes. Isn't it a valid scenario ? – Bilow Aug 18 '17 at 10:48
  • 3
    Nope. The answer to the question "Is this valid?" does not necessarily have to match the answers to the questions "Do I need this?", "Do I wish this was valid?" or even "Does this dirty hack work in current implementations of the spec?" – Spyryto Oct 23 '17 at 13:01
  • What would be an alternative in case you want to group some elements using an identifier? – Dawied May 08 '18 at 05:34
  • @Asimov: Classes or attributes. – SLaks May 08 '18 at 14:21
  • Under the current specification, the `id` attribute is for "uniquely identifying" a tag. Re-using the same `id` within the same document is considered invalid. https://dom.spec.whatwg.org/#concept-id – MLK.DEV Jul 31 '18 at 12:03
  • What about for something like bootstrap menu list items (
  • )? I have the same menu items, one that shows up in a standard menu, one that only shows up in an expanded menu in mobile view. They are, for all intents and purposes, the same item, and I want the same behavior for them if there is any object interaction. I have a plug in that isn't working because it won't assign an ID for the WordPress menu item in the mobile menu.
  • – PoloHoleSet Sep 11 '18 at 21:01
  • if I have two fields having same id, how to retrieve data collectively from those fields? Pardon me for so late comment, but I am having this issue for 2-3 hours and can't seem to find a working solution for it – Dhruv Singhal Oct 10 '18 at 13:08
  • @DhruvSinghal: Don't do that. IDs should be unique. – SLaks Oct 10 '18 at 14:13
  • OK so let me rephrase by question... I am NOT using ids. How can I achieve that now? – Dhruv Singhal Oct 10 '18 at 14:30
  • 1
    @DhruvSinghal: Use classes or `data-` attributes or anything else that can be selected by a CSS selector / `querySelector()`. – SLaks Oct 10 '18 at 17:00
  • Thanks, I used getElementByNames and got the desired result – Dhruv Singhal Oct 11 '18 at 07:08
  • @DhruvSinghal: `document.querySelectorAll('#foo')` could be used to retrieve all elements with same id's. (at least in Firefox) – user2988142 Oct 11 '18 at 14:54
  • @user2988142 as others mentioned, I will not be relying on duplicate IDs because I have to make the project browser independent. So getting elements by names has been better way for doing so for me (since I am using form) – Dhruv Singhal Oct 11 '18 at 15:25
  • @Spyryto There's a bit of an issue here in that the question title and the question body are asking different questions. To someone looking for an answer to the title, "Can multiple different HTML elements have the same ID if they're different elements?", the answer "no" is wrong, potentially dangerously so. In reality they absolutely can - but they're not supposed to, and indeed it is not valid for them to do so. – Richard Abey-Nesbit Aug 14 '20 at 00:49
  • this is a totally wrong answer why is it marked ? the answer is yes and it can be quite useful – kofifus Nov 18 '20 at 08:40