Can multiple different HTML elements have the same ID if they're different elements?












110















Is a scenario like this valid?:



div#foo
span#foo
a#foo









share|improve this question




















  • 19





    While sometimes possible, it's never valid.

    – Paul Creasey
    Apr 10 '11 at 13:16











  • 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
















110















Is a scenario like this valid?:



div#foo
span#foo
a#foo









share|improve this question




















  • 19





    While sometimes possible, it's never valid.

    – Paul Creasey
    Apr 10 '11 at 13:16











  • 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














110












110








110


22






Is a scenario like this valid?:



div#foo
span#foo
a#foo









share|improve this question
















Is a scenario like this valid?:



div#foo
span#foo
a#foo






html dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 '16 at 18:11







omninonsense

















asked Apr 10 '11 at 13:14









omninonsenseomninonsense

2,57873661




2,57873661








  • 19





    While sometimes possible, it's never valid.

    – Paul Creasey
    Apr 10 '11 at 13:16











  • 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














  • 19





    While sometimes possible, it's never valid.

    – Paul Creasey
    Apr 10 '11 at 13:16











  • 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








19




19





While sometimes possible, it's never valid.

– Paul Creasey
Apr 10 '11 at 13:16





While sometimes possible, it's never valid.

– Paul Creasey
Apr 10 '11 at 13:16













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





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












14 Answers
14






active

oldest

votes


















151














No.



Element IDs should be unique within the entire document.






share|improve this answer



















  • 67





    What are the consequences of not doing so?

    – corsiKa
    Dec 9 '12 at 5:11






  • 13





    @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








  • 56





    This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

    – BJury
    Jul 2 '14 at 11:33








  • 15





    why even use multiple similar id's when you got class for this purpose?

    – Max Yari
    Oct 14 '14 at 12:22






  • 3





    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





















72














I think there is a difference between whether something SHOULD be unique or MUST be unique (i.e. enforced by web browsers).



Should IDs be unique? YES.



Must IDs be unique? NO, at least IE and FireFox allow multiple elements to have the same ID.






share|improve this answer



















  • 6





    So does Chrome (v22 at the time this comment was written). :D

    – omninonsense
    Oct 25 '12 at 18:33






  • 23





    According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

    – leo
    Apr 9 '14 at 9:14








  • 2





    @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

    – BJury
    Jul 2 '14 at 11:39






  • 1





    In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

    – mltsy
    May 16 '17 at 18:32






  • 1





    Don't cry, use jQuery. @leo

    – Máxima Alekz
    Jun 6 '17 at 22:46



















33














Can multiple elements have the same ID?



Yes - whether they are the same tag or not, browsers will render the page even if multiple elements have the same ID.



Is it Valid HTML?



No. This is still true as of the HTML 5.1 spec. However, the spec also says getElementById must return the first element with the given ID, making the behavior not undefined in the case of an invalid document.



What are the consequences of this type of invalid HTML?



Most (if not all) browsers have and still do select the first element with a given ID, when calling getElementById. Most libraries that find elements by ID inherit this behavior. Most (if not all) browsers also apply styles assigned by id-selectors (e.g. #myid) to all elements with the specified ID. If this is what you expect and intend, then there are no unintended consequences. If you expect/intend something else (e.g. for all elements with that ID to be returned, or for the style to apply to only one element) then your expectations will not be met and any feature relying on those expectations will fail.



Some javascript libraries do have expectations that are not met when multiple elements have the same ID (see wootscootinboogie's comment about d3.js)



Conclusion



If you your code works as expected in your current environments, and these IDs are used in a predictable/maintainable way, then there are only 2 practical reasons to consider not to do this:




  1. To avoid the chance that you are wrong, and one of the libraries you use actually does malfunction when multiple elements have the same ID.

  2. To maintain forward-compatibility of your website/application with libraries or services (or developers!) that malfunction when multiple elements have the same ID, that you may encounter in the future.


The power is yours!






share|improve this answer


























  • Superb explanation...

    – Samim
    Jul 5 '18 at 7:04



















22














No. two elements with the same id are not valid. IDs are unique, if you wish to do something like that, use a class. Don't forget that elements can have multiple classes by using a space as a delimeter:



<div class="myclass sexy"></div>





share|improve this answer

































    22














    Even if the elements are of different types it can cause you some serious problems...



    Suppose you have 3 buttons with the same id:



    <button id="myid" data-mydata="this is button 1">button 1</button>
    <button id="myid" data-mydata="this is button 2">button 2</button>
    <button id="myid" data-mydata="this is button 3">button 3</button>


    Now you setup some jQuery code to do something when myid buttons are clicked:



    $(document).ready(function ()
    {
    $("#myid").click(function ()
    {
    var buttonData = $(this).data("mydata");

    // Call interesting function...
    interestingFunction();

    $('form').trigger('submit');
    });
    });


    What would you expect? That every button clicked would execute the click event handler setup with jQuery. Unfortunately it won't happen. ONLY the 1st button calls the click handler. The other 2 when clicked do nothing. It is as if they weren't buttons at all!



    So always assign different IDs to HTML elements. This will get you covered against strange things. :)



    <button id="button1" class="mybtn" data-mydata="this is button 1">button 1</button>
    <button id="button2" class="mybtn" data-mydata="this is button 2">button 2</button>
    <button id="button3" class="mybtn" data-mydata="this is button 3">button 3</button>


    Now if you want the click event handler to run when any of the buttons get clicked it will work perfectly if you change the selector in the jQuery code to use the CSS class applied to them like this:



    $(document).ready(function ()
    {
    $(".mybtn").click(function ()
    {
    var buttonData = $(this).data("mydata");

    // Call interesting function...
    interstingFunction();

    $('form').trigger('submit');
    });
    });





    share|improve this answer


























    • what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

      – Dmitry
      Aug 5 '16 at 4:42











    • This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

      – Karan Kaw
      Oct 11 '17 at 15:04



















    11














    The official spec for HTML states that id tags must be unique AND the official spec also states that if the render can be completed, it must (i.e. there are no such thing as "errors" in HTML, only "invalid" HTML). So, the following is how id tags actually work in practice. They are all invalid, but still work:



    This:



    <div id="unique">One</div>
    <div id="unique">Two</div>


    Renders fine in all browsers. However, document.getElementById only returns an object, not an array; you will only ever be able to select the first div via an id tag. If you were to change the id of the first div using JavaScript, the second ID would then be accessible with document.getElementById (tested on Chrome, FireFox & IE11). You can still select the div using other selection methods, and it's id property will be returned correctly.



    Please note this above issue opens a potential security vulnerability in sites that render SVG images, as SVGs are allowed to contain DOM elements, and also id tags on them (allows script DOM redirects via uploaded images). As long as the SVG is positioned in the DOM before the element it replaces, the image will receive all JavaScript events meant for the other element.



    This issue is currently not on anyone's radar as far as I know, yet it's real.



    This:



    <div id="unique" id="unique-also">One</div>


    Also renders fine in all browsers. However, only the first id you define this way is utilized, if you tried document.getElementById('unique-also'); in the above example, you would be returned null (tested on Chrome, FireFox & IE11).



    This:



    <div id="unique unique-two">Two</div>


    Also renders fine in all browsers, however, unlike class tags that can be separated by a space, the id tag allows spaces, so the id of the above element is actually "unique unique-two", and asking the dom for "unique" or "unique-two" in isolation returns null unless otherwise defined elsewhere in the DOM (tested on Chrome, FireFox & IE11).






    share|improve this answer





















    • 1





      "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

      – MrWhite
      Jan 30 at 15:28











    • I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

      – Nick Steele
      Jan 30 at 18:14





















    4














    SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.



    Example:



    (served to modern browsers) article#main-content {styled one way}

    (served to legacy) div#main-content {styled another way}



    Probably an antipattern though. Just leaving here as a devil's advocate point.






    share|improve this answer


























    • Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

      – psycho brm
      May 23 '17 at 8:31



















    3














    And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.



    e.g.



    <div id="one">first text for one</div>
    <div id="one">second text for one</div>


    and



    alert($('#one').size());


    See http://jsfiddle.net/RuysX/ for a test.






    share|improve this answer
























    • Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

      – Kevin B
      Apr 25 '13 at 21:16













    • Possibly this answer is true, I am saying this from experience.

      – Dibyanshu Jaiswal
      Feb 9 '15 at 11:39



















    2














    Well, using the HTML validator at w3.org, specific to HTML5, IDs must be unique



    Consider the following...



    <!DOCTYPE html> 
    <html>
    <head>
    <meta charset="UTF-8">
    <title>MyTitle</title>
    </head>
    <body>
    <div id="x">Barry</div>
    <div id="x">was</div>
    <div id="x">here</div>
    </body>
    </html>


    the validator responds with ...



    Line 9, Column 14: Duplicate ID x.      <div id="x">was</div>
    Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>
    Error Line 10, Column 14: Duplicate ID x. <div id="x">here</div>
    Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>


    ... but the OP specifically stated - what about different element types. So consider the following HTML...



    <!DOCTYPE html> 
    <html>
    <head>
    <meta charset="UTF-8">
    <title>MyTitle</title>
    </head>
    <body>
    <div id="x">barry
    <span id="x">was here</span>
    </div>
    </body>
    </html>


    ... the result from the validator is...



    Line 9, Column 16: Duplicate ID x.          <span id="x">was here</span>
    Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">barry


    Conclusion:



    In either case (same element type, or different element type), if the id is used more than once it is not considered valid HTML5.






    share|improve this answer































      0














      Nope, IDs have to be unique. You can use classes for that purpose



      <div class="a" /><div class="a b" /><span class="a" />

      div.a {font: ...;}
      /* or just: */
      .a {prop: value;}





      share|improve this answer































        0














        Is it possible to have more than one student in a class having same Roll/Id no? In HTMLid attribute is like so. You may use same class for them. e.g:



        <div class="a b c"></div>
        <div class="a b c d"></div>


        And so on.






        share|improve this answer































          0














          I think you can't do it because Id is unique you have to use it for one element . You can use class for the purpose






          share|improve this answer































            0

















            <div id="one">first text for one</div>
            <div id="one">second text for one</div>

            var ids = document.getElementById('one');





            ids contain only first div element. So even if there are multiple elements with the same id, the document object will return only first match.






            share|improve this answer































              0














              Usually, it is better not to use same ID multiple times in a html page.
              Even so, it is possible to use same ID many times in a page.
              However when you use an ID as a part of URI/URL as below:



              https://en.wikipedia.org/wiki/FIFA_World_Cup#2015_FIFA_corruption_case



              And if the ID('2015_FIFA_corruption_case') is used for only one (span)element in web page:



              <span class="mw-headline" id="2015_FIFA_corruption_case">2015 FIFA corruption case</span>


              There would be no problem. On the contrary, same ID exists in more than one place, the browser would be confused.






              share|improve this answer






















                protected by Mark Rotteveel Sep 6 '17 at 7:19



                Thank you for your interest in this question.
                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                Would you like to answer one of these unanswered questions instead?














                14 Answers
                14






                active

                oldest

                votes








                14 Answers
                14






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                151














                No.



                Element IDs should be unique within the entire document.






                share|improve this answer



















                • 67





                  What are the consequences of not doing so?

                  – corsiKa
                  Dec 9 '12 at 5:11






                • 13





                  @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








                • 56





                  This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                  – BJury
                  Jul 2 '14 at 11:33








                • 15





                  why even use multiple similar id's when you got class for this purpose?

                  – Max Yari
                  Oct 14 '14 at 12:22






                • 3





                  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


















                151














                No.



                Element IDs should be unique within the entire document.






                share|improve this answer



















                • 67





                  What are the consequences of not doing so?

                  – corsiKa
                  Dec 9 '12 at 5:11






                • 13





                  @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








                • 56





                  This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                  – BJury
                  Jul 2 '14 at 11:33








                • 15





                  why even use multiple similar id's when you got class for this purpose?

                  – Max Yari
                  Oct 14 '14 at 12:22






                • 3





                  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
















                151












                151








                151







                No.



                Element IDs should be unique within the entire document.






                share|improve this answer













                No.



                Element IDs should be unique within the entire document.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 10 '11 at 13:15









                SLaksSLaks

                685k13916401759




                685k13916401759








                • 67





                  What are the consequences of not doing so?

                  – corsiKa
                  Dec 9 '12 at 5:11






                • 13





                  @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








                • 56





                  This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                  – BJury
                  Jul 2 '14 at 11:33








                • 15





                  why even use multiple similar id's when you got class for this purpose?

                  – Max Yari
                  Oct 14 '14 at 12:22






                • 3





                  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
















                • 67





                  What are the consequences of not doing so?

                  – corsiKa
                  Dec 9 '12 at 5:11






                • 13





                  @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








                • 56





                  This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                  – BJury
                  Jul 2 '14 at 11:33








                • 15





                  why even use multiple similar id's when you got class for this purpose?

                  – Max Yari
                  Oct 14 '14 at 12:22






                • 3





                  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










                67




                67





                What are the consequences of not doing so?

                – corsiKa
                Dec 9 '12 at 5:11





                What are the consequences of not doing so?

                – corsiKa
                Dec 9 '12 at 5:11




                13




                13





                @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







                @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






                56




                56





                This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                – BJury
                Jul 2 '14 at 11:33







                This is incorrect. It is entirely possible to have multiple elements with the same ID. It's not generally best practice, but it does have its occasional uses. Everyone seems to cite how would selectors work, well if your going in knowing you'll have conflicting IDs, you use your selectors with a parent, where the IDs under the parent would be unique. eg $('div#car span#size) and $('div#truck span#size').

                – BJury
                Jul 2 '14 at 11:33






                15




                15





                why even use multiple similar id's when you got class for this purpose?

                – Max Yari
                Oct 14 '14 at 12:22





                why even use multiple similar id's when you got class for this purpose?

                – Max Yari
                Oct 14 '14 at 12:22




                3




                3





                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







                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















                72














                I think there is a difference between whether something SHOULD be unique or MUST be unique (i.e. enforced by web browsers).



                Should IDs be unique? YES.



                Must IDs be unique? NO, at least IE and FireFox allow multiple elements to have the same ID.






                share|improve this answer



















                • 6





                  So does Chrome (v22 at the time this comment was written). :D

                  – omninonsense
                  Oct 25 '12 at 18:33






                • 23





                  According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                  – leo
                  Apr 9 '14 at 9:14








                • 2





                  @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                  – BJury
                  Jul 2 '14 at 11:39






                • 1





                  In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                  – mltsy
                  May 16 '17 at 18:32






                • 1





                  Don't cry, use jQuery. @leo

                  – Máxima Alekz
                  Jun 6 '17 at 22:46
















                72














                I think there is a difference between whether something SHOULD be unique or MUST be unique (i.e. enforced by web browsers).



                Should IDs be unique? YES.



                Must IDs be unique? NO, at least IE and FireFox allow multiple elements to have the same ID.






                share|improve this answer



















                • 6





                  So does Chrome (v22 at the time this comment was written). :D

                  – omninonsense
                  Oct 25 '12 at 18:33






                • 23





                  According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                  – leo
                  Apr 9 '14 at 9:14








                • 2





                  @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                  – BJury
                  Jul 2 '14 at 11:39






                • 1





                  In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                  – mltsy
                  May 16 '17 at 18:32






                • 1





                  Don't cry, use jQuery. @leo

                  – Máxima Alekz
                  Jun 6 '17 at 22:46














                72












                72








                72







                I think there is a difference between whether something SHOULD be unique or MUST be unique (i.e. enforced by web browsers).



                Should IDs be unique? YES.



                Must IDs be unique? NO, at least IE and FireFox allow multiple elements to have the same ID.






                share|improve this answer













                I think there is a difference between whether something SHOULD be unique or MUST be unique (i.e. enforced by web browsers).



                Should IDs be unique? YES.



                Must IDs be unique? NO, at least IE and FireFox allow multiple elements to have the same ID.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 25 '12 at 18:27









                Jin KimJin Kim

                6,946134373




                6,946134373








                • 6





                  So does Chrome (v22 at the time this comment was written). :D

                  – omninonsense
                  Oct 25 '12 at 18:33






                • 23





                  According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                  – leo
                  Apr 9 '14 at 9:14








                • 2





                  @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                  – BJury
                  Jul 2 '14 at 11:39






                • 1





                  In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                  – mltsy
                  May 16 '17 at 18:32






                • 1





                  Don't cry, use jQuery. @leo

                  – Máxima Alekz
                  Jun 6 '17 at 22:46














                • 6





                  So does Chrome (v22 at the time this comment was written). :D

                  – omninonsense
                  Oct 25 '12 at 18:33






                • 23





                  According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                  – leo
                  Apr 9 '14 at 9:14








                • 2





                  @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                  – BJury
                  Jul 2 '14 at 11:39






                • 1





                  In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                  – mltsy
                  May 16 '17 at 18:32






                • 1





                  Don't cry, use jQuery. @leo

                  – Máxima Alekz
                  Jun 6 '17 at 22:46








                6




                6





                So does Chrome (v22 at the time this comment was written). :D

                – omninonsense
                Oct 25 '12 at 18:33





                So does Chrome (v22 at the time this comment was written). :D

                – omninonsense
                Oct 25 '12 at 18:33




                23




                23





                According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                – leo
                Apr 9 '14 at 9:14







                According to the spec, this is a MUST, not a SHOULD. (Does it still work in most browsers? Yes. Is it valid HTML? No. Also, for getElementById, the result in such cases is undefined, meaning that there is no way of telling how a browser might chose to handle it.)

                – leo
                Apr 9 '14 at 9:14






                2




                2





                @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                – BJury
                Jul 2 '14 at 11:39





                @leo however this is the real world where browsers don't conform fully to the standards. In this case it could be a good thing, as there is no reason to enforce unique IDs.

                – BJury
                Jul 2 '14 at 11:39




                1




                1





                In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                – mltsy
                May 16 '17 at 18:32





                In HTML5, the spec for getElementById actually does define that the first element with the given ID must be returned (which is how all browsers currently handle the situation anyway) - see my answer below for more.

                – mltsy
                May 16 '17 at 18:32




                1




                1





                Don't cry, use jQuery. @leo

                – Máxima Alekz
                Jun 6 '17 at 22:46





                Don't cry, use jQuery. @leo

                – Máxima Alekz
                Jun 6 '17 at 22:46











                33














                Can multiple elements have the same ID?



                Yes - whether they are the same tag or not, browsers will render the page even if multiple elements have the same ID.



                Is it Valid HTML?



                No. This is still true as of the HTML 5.1 spec. However, the spec also says getElementById must return the first element with the given ID, making the behavior not undefined in the case of an invalid document.



                What are the consequences of this type of invalid HTML?



                Most (if not all) browsers have and still do select the first element with a given ID, when calling getElementById. Most libraries that find elements by ID inherit this behavior. Most (if not all) browsers also apply styles assigned by id-selectors (e.g. #myid) to all elements with the specified ID. If this is what you expect and intend, then there are no unintended consequences. If you expect/intend something else (e.g. for all elements with that ID to be returned, or for the style to apply to only one element) then your expectations will not be met and any feature relying on those expectations will fail.



                Some javascript libraries do have expectations that are not met when multiple elements have the same ID (see wootscootinboogie's comment about d3.js)



                Conclusion



                If you your code works as expected in your current environments, and these IDs are used in a predictable/maintainable way, then there are only 2 practical reasons to consider not to do this:




                1. To avoid the chance that you are wrong, and one of the libraries you use actually does malfunction when multiple elements have the same ID.

                2. To maintain forward-compatibility of your website/application with libraries or services (or developers!) that malfunction when multiple elements have the same ID, that you may encounter in the future.


                The power is yours!






                share|improve this answer


























                • Superb explanation...

                  – Samim
                  Jul 5 '18 at 7:04
















                33














                Can multiple elements have the same ID?



                Yes - whether they are the same tag or not, browsers will render the page even if multiple elements have the same ID.



                Is it Valid HTML?



                No. This is still true as of the HTML 5.1 spec. However, the spec also says getElementById must return the first element with the given ID, making the behavior not undefined in the case of an invalid document.



                What are the consequences of this type of invalid HTML?



                Most (if not all) browsers have and still do select the first element with a given ID, when calling getElementById. Most libraries that find elements by ID inherit this behavior. Most (if not all) browsers also apply styles assigned by id-selectors (e.g. #myid) to all elements with the specified ID. If this is what you expect and intend, then there are no unintended consequences. If you expect/intend something else (e.g. for all elements with that ID to be returned, or for the style to apply to only one element) then your expectations will not be met and any feature relying on those expectations will fail.



                Some javascript libraries do have expectations that are not met when multiple elements have the same ID (see wootscootinboogie's comment about d3.js)



                Conclusion



                If you your code works as expected in your current environments, and these IDs are used in a predictable/maintainable way, then there are only 2 practical reasons to consider not to do this:




                1. To avoid the chance that you are wrong, and one of the libraries you use actually does malfunction when multiple elements have the same ID.

                2. To maintain forward-compatibility of your website/application with libraries or services (or developers!) that malfunction when multiple elements have the same ID, that you may encounter in the future.


                The power is yours!






                share|improve this answer


























                • Superb explanation...

                  – Samim
                  Jul 5 '18 at 7:04














                33












                33








                33







                Can multiple elements have the same ID?



                Yes - whether they are the same tag or not, browsers will render the page even if multiple elements have the same ID.



                Is it Valid HTML?



                No. This is still true as of the HTML 5.1 spec. However, the spec also says getElementById must return the first element with the given ID, making the behavior not undefined in the case of an invalid document.



                What are the consequences of this type of invalid HTML?



                Most (if not all) browsers have and still do select the first element with a given ID, when calling getElementById. Most libraries that find elements by ID inherit this behavior. Most (if not all) browsers also apply styles assigned by id-selectors (e.g. #myid) to all elements with the specified ID. If this is what you expect and intend, then there are no unintended consequences. If you expect/intend something else (e.g. for all elements with that ID to be returned, or for the style to apply to only one element) then your expectations will not be met and any feature relying on those expectations will fail.



                Some javascript libraries do have expectations that are not met when multiple elements have the same ID (see wootscootinboogie's comment about d3.js)



                Conclusion



                If you your code works as expected in your current environments, and these IDs are used in a predictable/maintainable way, then there are only 2 practical reasons to consider not to do this:




                1. To avoid the chance that you are wrong, and one of the libraries you use actually does malfunction when multiple elements have the same ID.

                2. To maintain forward-compatibility of your website/application with libraries or services (or developers!) that malfunction when multiple elements have the same ID, that you may encounter in the future.


                The power is yours!






                share|improve this answer















                Can multiple elements have the same ID?



                Yes - whether they are the same tag or not, browsers will render the page even if multiple elements have the same ID.



                Is it Valid HTML?



                No. This is still true as of the HTML 5.1 spec. However, the spec also says getElementById must return the first element with the given ID, making the behavior not undefined in the case of an invalid document.



                What are the consequences of this type of invalid HTML?



                Most (if not all) browsers have and still do select the first element with a given ID, when calling getElementById. Most libraries that find elements by ID inherit this behavior. Most (if not all) browsers also apply styles assigned by id-selectors (e.g. #myid) to all elements with the specified ID. If this is what you expect and intend, then there are no unintended consequences. If you expect/intend something else (e.g. for all elements with that ID to be returned, or for the style to apply to only one element) then your expectations will not be met and any feature relying on those expectations will fail.



                Some javascript libraries do have expectations that are not met when multiple elements have the same ID (see wootscootinboogie's comment about d3.js)



                Conclusion



                If you your code works as expected in your current environments, and these IDs are used in a predictable/maintainable way, then there are only 2 practical reasons to consider not to do this:




                1. To avoid the chance that you are wrong, and one of the libraries you use actually does malfunction when multiple elements have the same ID.

                2. To maintain forward-compatibility of your website/application with libraries or services (or developers!) that malfunction when multiple elements have the same ID, that you may encounter in the future.


                The power is yours!







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 6 '17 at 20:15

























                answered May 16 '17 at 18:30









                mltsymltsy

                3,1862234




                3,1862234













                • Superb explanation...

                  – Samim
                  Jul 5 '18 at 7:04



















                • Superb explanation...

                  – Samim
                  Jul 5 '18 at 7:04

















                Superb explanation...

                – Samim
                Jul 5 '18 at 7:04





                Superb explanation...

                – Samim
                Jul 5 '18 at 7:04











                22














                No. two elements with the same id are not valid. IDs are unique, if you wish to do something like that, use a class. Don't forget that elements can have multiple classes by using a space as a delimeter:



                <div class="myclass sexy"></div>





                share|improve this answer






























                  22














                  No. two elements with the same id are not valid. IDs are unique, if you wish to do something like that, use a class. Don't forget that elements can have multiple classes by using a space as a delimeter:



                  <div class="myclass sexy"></div>





                  share|improve this answer




























                    22












                    22








                    22







                    No. two elements with the same id are not valid. IDs are unique, if you wish to do something like that, use a class. Don't forget that elements can have multiple classes by using a space as a delimeter:



                    <div class="myclass sexy"></div>





                    share|improve this answer















                    No. two elements with the same id are not valid. IDs are unique, if you wish to do something like that, use a class. Don't forget that elements can have multiple classes by using a space as a delimeter:



                    <div class="myclass sexy"></div>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 30 '12 at 17:34

























                    answered Apr 10 '11 at 13:15









                    GazlerGazler

                    68.6k13222218




                    68.6k13222218























                        22














                        Even if the elements are of different types it can cause you some serious problems...



                        Suppose you have 3 buttons with the same id:



                        <button id="myid" data-mydata="this is button 1">button 1</button>
                        <button id="myid" data-mydata="this is button 2">button 2</button>
                        <button id="myid" data-mydata="this is button 3">button 3</button>


                        Now you setup some jQuery code to do something when myid buttons are clicked:



                        $(document).ready(function ()
                        {
                        $("#myid").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interestingFunction();

                        $('form').trigger('submit');
                        });
                        });


                        What would you expect? That every button clicked would execute the click event handler setup with jQuery. Unfortunately it won't happen. ONLY the 1st button calls the click handler. The other 2 when clicked do nothing. It is as if they weren't buttons at all!



                        So always assign different IDs to HTML elements. This will get you covered against strange things. :)



                        <button id="button1" class="mybtn" data-mydata="this is button 1">button 1</button>
                        <button id="button2" class="mybtn" data-mydata="this is button 2">button 2</button>
                        <button id="button3" class="mybtn" data-mydata="this is button 3">button 3</button>


                        Now if you want the click event handler to run when any of the buttons get clicked it will work perfectly if you change the selector in the jQuery code to use the CSS class applied to them like this:



                        $(document).ready(function ()
                        {
                        $(".mybtn").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interstingFunction();

                        $('form').trigger('submit');
                        });
                        });





                        share|improve this answer


























                        • what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                          – Dmitry
                          Aug 5 '16 at 4:42











                        • This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                          – Karan Kaw
                          Oct 11 '17 at 15:04
















                        22














                        Even if the elements are of different types it can cause you some serious problems...



                        Suppose you have 3 buttons with the same id:



                        <button id="myid" data-mydata="this is button 1">button 1</button>
                        <button id="myid" data-mydata="this is button 2">button 2</button>
                        <button id="myid" data-mydata="this is button 3">button 3</button>


                        Now you setup some jQuery code to do something when myid buttons are clicked:



                        $(document).ready(function ()
                        {
                        $("#myid").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interestingFunction();

                        $('form').trigger('submit');
                        });
                        });


                        What would you expect? That every button clicked would execute the click event handler setup with jQuery. Unfortunately it won't happen. ONLY the 1st button calls the click handler. The other 2 when clicked do nothing. It is as if they weren't buttons at all!



                        So always assign different IDs to HTML elements. This will get you covered against strange things. :)



                        <button id="button1" class="mybtn" data-mydata="this is button 1">button 1</button>
                        <button id="button2" class="mybtn" data-mydata="this is button 2">button 2</button>
                        <button id="button3" class="mybtn" data-mydata="this is button 3">button 3</button>


                        Now if you want the click event handler to run when any of the buttons get clicked it will work perfectly if you change the selector in the jQuery code to use the CSS class applied to them like this:



                        $(document).ready(function ()
                        {
                        $(".mybtn").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interstingFunction();

                        $('form').trigger('submit');
                        });
                        });





                        share|improve this answer


























                        • what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                          – Dmitry
                          Aug 5 '16 at 4:42











                        • This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                          – Karan Kaw
                          Oct 11 '17 at 15:04














                        22












                        22








                        22







                        Even if the elements are of different types it can cause you some serious problems...



                        Suppose you have 3 buttons with the same id:



                        <button id="myid" data-mydata="this is button 1">button 1</button>
                        <button id="myid" data-mydata="this is button 2">button 2</button>
                        <button id="myid" data-mydata="this is button 3">button 3</button>


                        Now you setup some jQuery code to do something when myid buttons are clicked:



                        $(document).ready(function ()
                        {
                        $("#myid").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interestingFunction();

                        $('form').trigger('submit');
                        });
                        });


                        What would you expect? That every button clicked would execute the click event handler setup with jQuery. Unfortunately it won't happen. ONLY the 1st button calls the click handler. The other 2 when clicked do nothing. It is as if they weren't buttons at all!



                        So always assign different IDs to HTML elements. This will get you covered against strange things. :)



                        <button id="button1" class="mybtn" data-mydata="this is button 1">button 1</button>
                        <button id="button2" class="mybtn" data-mydata="this is button 2">button 2</button>
                        <button id="button3" class="mybtn" data-mydata="this is button 3">button 3</button>


                        Now if you want the click event handler to run when any of the buttons get clicked it will work perfectly if you change the selector in the jQuery code to use the CSS class applied to them like this:



                        $(document).ready(function ()
                        {
                        $(".mybtn").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interstingFunction();

                        $('form').trigger('submit');
                        });
                        });





                        share|improve this answer















                        Even if the elements are of different types it can cause you some serious problems...



                        Suppose you have 3 buttons with the same id:



                        <button id="myid" data-mydata="this is button 1">button 1</button>
                        <button id="myid" data-mydata="this is button 2">button 2</button>
                        <button id="myid" data-mydata="this is button 3">button 3</button>


                        Now you setup some jQuery code to do something when myid buttons are clicked:



                        $(document).ready(function ()
                        {
                        $("#myid").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interestingFunction();

                        $('form').trigger('submit');
                        });
                        });


                        What would you expect? That every button clicked would execute the click event handler setup with jQuery. Unfortunately it won't happen. ONLY the 1st button calls the click handler. The other 2 when clicked do nothing. It is as if they weren't buttons at all!



                        So always assign different IDs to HTML elements. This will get you covered against strange things. :)



                        <button id="button1" class="mybtn" data-mydata="this is button 1">button 1</button>
                        <button id="button2" class="mybtn" data-mydata="this is button 2">button 2</button>
                        <button id="button3" class="mybtn" data-mydata="this is button 3">button 3</button>


                        Now if you want the click event handler to run when any of the buttons get clicked it will work perfectly if you change the selector in the jQuery code to use the CSS class applied to them like this:



                        $(document).ready(function ()
                        {
                        $(".mybtn").click(function ()
                        {
                        var buttonData = $(this).data("mydata");

                        // Call interesting function...
                        interstingFunction();

                        $('form').trigger('submit');
                        });
                        });






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 3 '15 at 3:09









                        bfred.it

                        14.7k56893




                        14.7k56893










                        answered Jul 23 '13 at 6:22









                        Leniel MaccaferriLeniel Maccaferri

                        80.4k34287394




                        80.4k34287394













                        • what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                          – Dmitry
                          Aug 5 '16 at 4:42











                        • This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                          – Karan Kaw
                          Oct 11 '17 at 15:04



















                        • what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                          – Dmitry
                          Aug 5 '16 at 4:42











                        • This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                          – Karan Kaw
                          Oct 11 '17 at 15:04

















                        what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                        – Dmitry
                        Aug 5 '16 at 4:42





                        what if I have a "#content" which I already have referenced in a variable, and a #my-div #content which I only have for a few moments after which I remove the referenced node and forget its variable, after which the #div #content performs a myDiv.outerHTML = myDiv.innerHTML to replace the original. This saves the need to hard copy all styles and contents of #content into #decoy and do the same thing. This makes sense when doing transitions.

                        – Dmitry
                        Aug 5 '16 at 4:42













                        This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                        – Karan Kaw
                        Oct 11 '17 at 15:04





                        This means that, even if I use 'append' to add multiple element of same id, DOM only considers first element to be real, ideally 1 ID = 1 Element

                        – Karan Kaw
                        Oct 11 '17 at 15:04











                        11














                        The official spec for HTML states that id tags must be unique AND the official spec also states that if the render can be completed, it must (i.e. there are no such thing as "errors" in HTML, only "invalid" HTML). So, the following is how id tags actually work in practice. They are all invalid, but still work:



                        This:



                        <div id="unique">One</div>
                        <div id="unique">Two</div>


                        Renders fine in all browsers. However, document.getElementById only returns an object, not an array; you will only ever be able to select the first div via an id tag. If you were to change the id of the first div using JavaScript, the second ID would then be accessible with document.getElementById (tested on Chrome, FireFox & IE11). You can still select the div using other selection methods, and it's id property will be returned correctly.



                        Please note this above issue opens a potential security vulnerability in sites that render SVG images, as SVGs are allowed to contain DOM elements, and also id tags on them (allows script DOM redirects via uploaded images). As long as the SVG is positioned in the DOM before the element it replaces, the image will receive all JavaScript events meant for the other element.



                        This issue is currently not on anyone's radar as far as I know, yet it's real.



                        This:



                        <div id="unique" id="unique-also">One</div>


                        Also renders fine in all browsers. However, only the first id you define this way is utilized, if you tried document.getElementById('unique-also'); in the above example, you would be returned null (tested on Chrome, FireFox & IE11).



                        This:



                        <div id="unique unique-two">Two</div>


                        Also renders fine in all browsers, however, unlike class tags that can be separated by a space, the id tag allows spaces, so the id of the above element is actually "unique unique-two", and asking the dom for "unique" or "unique-two" in isolation returns null unless otherwise defined elsewhere in the DOM (tested on Chrome, FireFox & IE11).






                        share|improve this answer





















                        • 1





                          "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                          – MrWhite
                          Jan 30 at 15:28











                        • I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                          – Nick Steele
                          Jan 30 at 18:14


















                        11














                        The official spec for HTML states that id tags must be unique AND the official spec also states that if the render can be completed, it must (i.e. there are no such thing as "errors" in HTML, only "invalid" HTML). So, the following is how id tags actually work in practice. They are all invalid, but still work:



                        This:



                        <div id="unique">One</div>
                        <div id="unique">Two</div>


                        Renders fine in all browsers. However, document.getElementById only returns an object, not an array; you will only ever be able to select the first div via an id tag. If you were to change the id of the first div using JavaScript, the second ID would then be accessible with document.getElementById (tested on Chrome, FireFox & IE11). You can still select the div using other selection methods, and it's id property will be returned correctly.



                        Please note this above issue opens a potential security vulnerability in sites that render SVG images, as SVGs are allowed to contain DOM elements, and also id tags on them (allows script DOM redirects via uploaded images). As long as the SVG is positioned in the DOM before the element it replaces, the image will receive all JavaScript events meant for the other element.



                        This issue is currently not on anyone's radar as far as I know, yet it's real.



                        This:



                        <div id="unique" id="unique-also">One</div>


                        Also renders fine in all browsers. However, only the first id you define this way is utilized, if you tried document.getElementById('unique-also'); in the above example, you would be returned null (tested on Chrome, FireFox & IE11).



                        This:



                        <div id="unique unique-two">Two</div>


                        Also renders fine in all browsers, however, unlike class tags that can be separated by a space, the id tag allows spaces, so the id of the above element is actually "unique unique-two", and asking the dom for "unique" or "unique-two" in isolation returns null unless otherwise defined elsewhere in the DOM (tested on Chrome, FireFox & IE11).






                        share|improve this answer





















                        • 1





                          "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                          – MrWhite
                          Jan 30 at 15:28











                        • I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                          – Nick Steele
                          Jan 30 at 18:14
















                        11












                        11








                        11







                        The official spec for HTML states that id tags must be unique AND the official spec also states that if the render can be completed, it must (i.e. there are no such thing as "errors" in HTML, only "invalid" HTML). So, the following is how id tags actually work in practice. They are all invalid, but still work:



                        This:



                        <div id="unique">One</div>
                        <div id="unique">Two</div>


                        Renders fine in all browsers. However, document.getElementById only returns an object, not an array; you will only ever be able to select the first div via an id tag. If you were to change the id of the first div using JavaScript, the second ID would then be accessible with document.getElementById (tested on Chrome, FireFox & IE11). You can still select the div using other selection methods, and it's id property will be returned correctly.



                        Please note this above issue opens a potential security vulnerability in sites that render SVG images, as SVGs are allowed to contain DOM elements, and also id tags on them (allows script DOM redirects via uploaded images). As long as the SVG is positioned in the DOM before the element it replaces, the image will receive all JavaScript events meant for the other element.



                        This issue is currently not on anyone's radar as far as I know, yet it's real.



                        This:



                        <div id="unique" id="unique-also">One</div>


                        Also renders fine in all browsers. However, only the first id you define this way is utilized, if you tried document.getElementById('unique-also'); in the above example, you would be returned null (tested on Chrome, FireFox & IE11).



                        This:



                        <div id="unique unique-two">Two</div>


                        Also renders fine in all browsers, however, unlike class tags that can be separated by a space, the id tag allows spaces, so the id of the above element is actually "unique unique-two", and asking the dom for "unique" or "unique-two" in isolation returns null unless otherwise defined elsewhere in the DOM (tested on Chrome, FireFox & IE11).






                        share|improve this answer















                        The official spec for HTML states that id tags must be unique AND the official spec also states that if the render can be completed, it must (i.e. there are no such thing as "errors" in HTML, only "invalid" HTML). So, the following is how id tags actually work in practice. They are all invalid, but still work:



                        This:



                        <div id="unique">One</div>
                        <div id="unique">Two</div>


                        Renders fine in all browsers. However, document.getElementById only returns an object, not an array; you will only ever be able to select the first div via an id tag. If you were to change the id of the first div using JavaScript, the second ID would then be accessible with document.getElementById (tested on Chrome, FireFox & IE11). You can still select the div using other selection methods, and it's id property will be returned correctly.



                        Please note this above issue opens a potential security vulnerability in sites that render SVG images, as SVGs are allowed to contain DOM elements, and also id tags on them (allows script DOM redirects via uploaded images). As long as the SVG is positioned in the DOM before the element it replaces, the image will receive all JavaScript events meant for the other element.



                        This issue is currently not on anyone's radar as far as I know, yet it's real.



                        This:



                        <div id="unique" id="unique-also">One</div>


                        Also renders fine in all browsers. However, only the first id you define this way is utilized, if you tried document.getElementById('unique-also'); in the above example, you would be returned null (tested on Chrome, FireFox & IE11).



                        This:



                        <div id="unique unique-two">Two</div>


                        Also renders fine in all browsers, however, unlike class tags that can be separated by a space, the id tag allows spaces, so the id of the above element is actually "unique unique-two", and asking the dom for "unique" or "unique-two" in isolation returns null unless otherwise defined elsewhere in the DOM (tested on Chrome, FireFox & IE11).







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 15 '16 at 13:50

























                        answered Sep 8 '16 at 13:52









                        Nick SteeleNick Steele

                        3,88222022




                        3,88222022








                        • 1





                          "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                          – MrWhite
                          Jan 30 at 15:28











                        • I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                          – Nick Steele
                          Jan 30 at 18:14
















                        • 1





                          "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                          – MrWhite
                          Jan 30 at 15:28











                        • I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                          – Nick Steele
                          Jan 30 at 18:14










                        1




                        1





                        "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                        – MrWhite
                        Jan 30 at 15:28





                        "the id tag allows spaces" - Although, according to the spec, the "The value must not contain any space characters."

                        – MrWhite
                        Jan 30 at 15:28













                        I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                        – Nick Steele
                        Jan 30 at 18:14







                        I agree. However, there is the spec, and there is how the browsers operate. Browsers historically treat the spec as something of a goal, but have not been strict on many of the items. I think they do this because if they met the spec it would break lots of existing sites or something. I mention at top that although these things work, they are invalid.

                        – Nick Steele
                        Jan 30 at 18:14













                        4














                        SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.



                        Example:



                        (served to modern browsers) article#main-content {styled one way}

                        (served to legacy) div#main-content {styled another way}



                        Probably an antipattern though. Just leaving here as a devil's advocate point.






                        share|improve this answer


























                        • Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                          – psycho brm
                          May 23 '17 at 8:31
















                        4














                        SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.



                        Example:



                        (served to modern browsers) article#main-content {styled one way}

                        (served to legacy) div#main-content {styled another way}



                        Probably an antipattern though. Just leaving here as a devil's advocate point.






                        share|improve this answer


























                        • Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                          – psycho brm
                          May 23 '17 at 8:31














                        4












                        4








                        4







                        SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.



                        Example:



                        (served to modern browsers) article#main-content {styled one way}

                        (served to legacy) div#main-content {styled another way}



                        Probably an antipattern though. Just leaving here as a devil's advocate point.






                        share|improve this answer















                        SLaks answer is correct, but as an addendum note that the x/html specs specify that all ids must be unique within a (single) html document. Although it's not exactly what the op asked, there could be valid instances where the same id is attached to different entities across multiple pages.



                        Example:



                        (served to modern browsers) article#main-content {styled one way}

                        (served to legacy) div#main-content {styled another way}



                        Probably an antipattern though. Just leaving here as a devil's advocate point.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 14 '13 at 13:46

























                        answered Sep 14 '11 at 20:03









                        RobWRobW

                        6,75333437




                        6,75333437













                        • Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                          – psycho brm
                          May 23 '17 at 8:31



















                        • Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                          – psycho brm
                          May 23 '17 at 8:31

















                        Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                        – psycho brm
                        May 23 '17 at 8:31





                        Good point. Although the dynamically generated content that is supposed to be inserted into another page should avoid ids altogether. Ids are like globals in programming languages, you can use them, and there are valid cases where it's a nice hack that simplifies things. It's a nice practice to consider doing things right before doing hacks.

                        – psycho brm
                        May 23 '17 at 8:31











                        3














                        And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.



                        e.g.



                        <div id="one">first text for one</div>
                        <div id="one">second text for one</div>


                        and



                        alert($('#one').size());


                        See http://jsfiddle.net/RuysX/ for a test.






                        share|improve this answer
























                        • Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                          – Kevin B
                          Apr 25 '13 at 21:16













                        • Possibly this answer is true, I am saying this from experience.

                          – Dibyanshu Jaiswal
                          Feb 9 '15 at 11:39
















                        3














                        And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.



                        e.g.



                        <div id="one">first text for one</div>
                        <div id="one">second text for one</div>


                        and



                        alert($('#one').size());


                        See http://jsfiddle.net/RuysX/ for a test.






                        share|improve this answer
























                        • Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                          – Kevin B
                          Apr 25 '13 at 21:16













                        • Possibly this answer is true, I am saying this from experience.

                          – Dibyanshu Jaiswal
                          Feb 9 '15 at 11:39














                        3












                        3








                        3







                        And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.



                        e.g.



                        <div id="one">first text for one</div>
                        <div id="one">second text for one</div>


                        and



                        alert($('#one').size());


                        See http://jsfiddle.net/RuysX/ for a test.






                        share|improve this answer













                        And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.



                        e.g.



                        <div id="one">first text for one</div>
                        <div id="one">second text for one</div>


                        and



                        alert($('#one').size());


                        See http://jsfiddle.net/RuysX/ for a test.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 21 '13 at 22:43









                        denishaskindenishaskin

                        1,25531330




                        1,25531330













                        • Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                          – Kevin B
                          Apr 25 '13 at 21:16













                        • Possibly this answer is true, I am saying this from experience.

                          – Dibyanshu Jaiswal
                          Feb 9 '15 at 11:39



















                        • Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                          – Kevin B
                          Apr 25 '13 at 21:16













                        • Possibly this answer is true, I am saying this from experience.

                          – Dibyanshu Jaiswal
                          Feb 9 '15 at 11:39

















                        Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                        – Kevin B
                        Apr 25 '13 at 21:16







                        Unless you use a more complex selector, such as div#one That of course doesn't change the fact that it's invalid.

                        – Kevin B
                        Apr 25 '13 at 21:16















                        Possibly this answer is true, I am saying this from experience.

                        – Dibyanshu Jaiswal
                        Feb 9 '15 at 11:39





                        Possibly this answer is true, I am saying this from experience.

                        – Dibyanshu Jaiswal
                        Feb 9 '15 at 11:39











                        2














                        Well, using the HTML validator at w3.org, specific to HTML5, IDs must be unique



                        Consider the following...



                        <!DOCTYPE html> 
                        <html>
                        <head>
                        <meta charset="UTF-8">
                        <title>MyTitle</title>
                        </head>
                        <body>
                        <div id="x">Barry</div>
                        <div id="x">was</div>
                        <div id="x">here</div>
                        </body>
                        </html>


                        the validator responds with ...



                        Line 9, Column 14: Duplicate ID x.      <div id="x">was</div>
                        Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>
                        Error Line 10, Column 14: Duplicate ID x. <div id="x">here</div>
                        Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>


                        ... but the OP specifically stated - what about different element types. So consider the following HTML...



                        <!DOCTYPE html> 
                        <html>
                        <head>
                        <meta charset="UTF-8">
                        <title>MyTitle</title>
                        </head>
                        <body>
                        <div id="x">barry
                        <span id="x">was here</span>
                        </div>
                        </body>
                        </html>


                        ... the result from the validator is...



                        Line 9, Column 16: Duplicate ID x.          <span id="x">was here</span>
                        Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">barry


                        Conclusion:



                        In either case (same element type, or different element type), if the id is used more than once it is not considered valid HTML5.






                        share|improve this answer




























                          2














                          Well, using the HTML validator at w3.org, specific to HTML5, IDs must be unique



                          Consider the following...



                          <!DOCTYPE html> 
                          <html>
                          <head>
                          <meta charset="UTF-8">
                          <title>MyTitle</title>
                          </head>
                          <body>
                          <div id="x">Barry</div>
                          <div id="x">was</div>
                          <div id="x">here</div>
                          </body>
                          </html>


                          the validator responds with ...



                          Line 9, Column 14: Duplicate ID x.      <div id="x">was</div>
                          Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>
                          Error Line 10, Column 14: Duplicate ID x. <div id="x">here</div>
                          Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>


                          ... but the OP specifically stated - what about different element types. So consider the following HTML...



                          <!DOCTYPE html> 
                          <html>
                          <head>
                          <meta charset="UTF-8">
                          <title>MyTitle</title>
                          </head>
                          <body>
                          <div id="x">barry
                          <span id="x">was here</span>
                          </div>
                          </body>
                          </html>


                          ... the result from the validator is...



                          Line 9, Column 16: Duplicate ID x.          <span id="x">was here</span>
                          Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">barry


                          Conclusion:



                          In either case (same element type, or different element type), if the id is used more than once it is not considered valid HTML5.






                          share|improve this answer


























                            2












                            2








                            2







                            Well, using the HTML validator at w3.org, specific to HTML5, IDs must be unique



                            Consider the following...



                            <!DOCTYPE html> 
                            <html>
                            <head>
                            <meta charset="UTF-8">
                            <title>MyTitle</title>
                            </head>
                            <body>
                            <div id="x">Barry</div>
                            <div id="x">was</div>
                            <div id="x">here</div>
                            </body>
                            </html>


                            the validator responds with ...



                            Line 9, Column 14: Duplicate ID x.      <div id="x">was</div>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>
                            Error Line 10, Column 14: Duplicate ID x. <div id="x">here</div>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>


                            ... but the OP specifically stated - what about different element types. So consider the following HTML...



                            <!DOCTYPE html> 
                            <html>
                            <head>
                            <meta charset="UTF-8">
                            <title>MyTitle</title>
                            </head>
                            <body>
                            <div id="x">barry
                            <span id="x">was here</span>
                            </div>
                            </body>
                            </html>


                            ... the result from the validator is...



                            Line 9, Column 16: Duplicate ID x.          <span id="x">was here</span>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">barry


                            Conclusion:



                            In either case (same element type, or different element type), if the id is used more than once it is not considered valid HTML5.






                            share|improve this answer













                            Well, using the HTML validator at w3.org, specific to HTML5, IDs must be unique



                            Consider the following...



                            <!DOCTYPE html> 
                            <html>
                            <head>
                            <meta charset="UTF-8">
                            <title>MyTitle</title>
                            </head>
                            <body>
                            <div id="x">Barry</div>
                            <div id="x">was</div>
                            <div id="x">here</div>
                            </body>
                            </html>


                            the validator responds with ...



                            Line 9, Column 14: Duplicate ID x.      <div id="x">was</div>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>
                            Error Line 10, Column 14: Duplicate ID x. <div id="x">here</div>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">Barry</div>


                            ... but the OP specifically stated - what about different element types. So consider the following HTML...



                            <!DOCTYPE html> 
                            <html>
                            <head>
                            <meta charset="UTF-8">
                            <title>MyTitle</title>
                            </head>
                            <body>
                            <div id="x">barry
                            <span id="x">was here</span>
                            </div>
                            </body>
                            </html>


                            ... the result from the validator is...



                            Line 9, Column 16: Duplicate ID x.          <span id="x">was here</span>
                            Warning Line 8, Column 14: The first occurrence of ID x was here. <div id="x">barry


                            Conclusion:



                            In either case (same element type, or different element type), if the id is used more than once it is not considered valid HTML5.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '14 at 16:45









                            barrypickerbarrypicker

                            4,41624153




                            4,41624153























                                0














                                Nope, IDs have to be unique. You can use classes for that purpose



                                <div class="a" /><div class="a b" /><span class="a" />

                                div.a {font: ...;}
                                /* or just: */
                                .a {prop: value;}





                                share|improve this answer




























                                  0














                                  Nope, IDs have to be unique. You can use classes for that purpose



                                  <div class="a" /><div class="a b" /><span class="a" />

                                  div.a {font: ...;}
                                  /* or just: */
                                  .a {prop: value;}





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Nope, IDs have to be unique. You can use classes for that purpose



                                    <div class="a" /><div class="a b" /><span class="a" />

                                    div.a {font: ...;}
                                    /* or just: */
                                    .a {prop: value;}





                                    share|improve this answer













                                    Nope, IDs have to be unique. You can use classes for that purpose



                                    <div class="a" /><div class="a b" /><span class="a" />

                                    div.a {font: ...;}
                                    /* or just: */
                                    .a {prop: value;}






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Apr 10 '11 at 13:16









                                    phihagphihag

                                    198k48363407




                                    198k48363407























                                        0














                                        Is it possible to have more than one student in a class having same Roll/Id no? In HTMLid attribute is like so. You may use same class for them. e.g:



                                        <div class="a b c"></div>
                                        <div class="a b c d"></div>


                                        And so on.






                                        share|improve this answer




























                                          0














                                          Is it possible to have more than one student in a class having same Roll/Id no? In HTMLid attribute is like so. You may use same class for them. e.g:



                                          <div class="a b c"></div>
                                          <div class="a b c d"></div>


                                          And so on.






                                          share|improve this answer


























                                            0












                                            0








                                            0







                                            Is it possible to have more than one student in a class having same Roll/Id no? In HTMLid attribute is like so. You may use same class for them. e.g:



                                            <div class="a b c"></div>
                                            <div class="a b c d"></div>


                                            And so on.






                                            share|improve this answer













                                            Is it possible to have more than one student in a class having same Roll/Id no? In HTMLid attribute is like so. You may use same class for them. e.g:



                                            <div class="a b c"></div>
                                            <div class="a b c d"></div>


                                            And so on.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Apr 10 '11 at 13:19









                                            thecodeparadoxthecodeparadox

                                            70.8k18121152




                                            70.8k18121152























                                                0














                                                I think you can't do it because Id is unique you have to use it for one element . You can use class for the purpose






                                                share|improve this answer




























                                                  0














                                                  I think you can't do it because Id is unique you have to use it for one element . You can use class for the purpose






                                                  share|improve this answer


























                                                    0












                                                    0








                                                    0







                                                    I think you can't do it because Id is unique you have to use it for one element . You can use class for the purpose






                                                    share|improve this answer













                                                    I think you can't do it because Id is unique you have to use it for one element . You can use class for the purpose







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Sep 6 '17 at 6:26









                                                    janaravijanaravi

                                                    777




                                                    777























                                                        0

















                                                        <div id="one">first text for one</div>
                                                        <div id="one">second text for one</div>

                                                        var ids = document.getElementById('one');





                                                        ids contain only first div element. So even if there are multiple elements with the same id, the document object will return only first match.






                                                        share|improve this answer




























                                                          0

















                                                          <div id="one">first text for one</div>
                                                          <div id="one">second text for one</div>

                                                          var ids = document.getElementById('one');





                                                          ids contain only first div element. So even if there are multiple elements with the same id, the document object will return only first match.






                                                          share|improve this answer


























                                                            0












                                                            0








                                                            0










                                                            <div id="one">first text for one</div>
                                                            <div id="one">second text for one</div>

                                                            var ids = document.getElementById('one');





                                                            ids contain only first div element. So even if there are multiple elements with the same id, the document object will return only first match.






                                                            share|improve this answer
















                                                            <div id="one">first text for one</div>
                                                            <div id="one">second text for one</div>

                                                            var ids = document.getElementById('one');





                                                            ids contain only first div element. So even if there are multiple elements with the same id, the document object will return only first match.






                                                            <div id="one">first text for one</div>
                                                            <div id="one">second text for one</div>

                                                            var ids = document.getElementById('one');





                                                            <div id="one">first text for one</div>
                                                            <div id="one">second text for one</div>

                                                            var ids = document.getElementById('one');






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Jan 12 '18 at 16:19









                                                            ganesh phirkeganesh phirke

                                                            535




                                                            535























                                                                0














                                                                Usually, it is better not to use same ID multiple times in a html page.
                                                                Even so, it is possible to use same ID many times in a page.
                                                                However when you use an ID as a part of URI/URL as below:



                                                                https://en.wikipedia.org/wiki/FIFA_World_Cup#2015_FIFA_corruption_case



                                                                And if the ID('2015_FIFA_corruption_case') is used for only one (span)element in web page:



                                                                <span class="mw-headline" id="2015_FIFA_corruption_case">2015 FIFA corruption case</span>


                                                                There would be no problem. On the contrary, same ID exists in more than one place, the browser would be confused.






                                                                share|improve this answer




























                                                                  0














                                                                  Usually, it is better not to use same ID multiple times in a html page.
                                                                  Even so, it is possible to use same ID many times in a page.
                                                                  However when you use an ID as a part of URI/URL as below:



                                                                  https://en.wikipedia.org/wiki/FIFA_World_Cup#2015_FIFA_corruption_case



                                                                  And if the ID('2015_FIFA_corruption_case') is used for only one (span)element in web page:



                                                                  <span class="mw-headline" id="2015_FIFA_corruption_case">2015 FIFA corruption case</span>


                                                                  There would be no problem. On the contrary, same ID exists in more than one place, the browser would be confused.






                                                                  share|improve this answer


























                                                                    0












                                                                    0








                                                                    0







                                                                    Usually, it is better not to use same ID multiple times in a html page.
                                                                    Even so, it is possible to use same ID many times in a page.
                                                                    However when you use an ID as a part of URI/URL as below:



                                                                    https://en.wikipedia.org/wiki/FIFA_World_Cup#2015_FIFA_corruption_case



                                                                    And if the ID('2015_FIFA_corruption_case') is used for only one (span)element in web page:



                                                                    <span class="mw-headline" id="2015_FIFA_corruption_case">2015 FIFA corruption case</span>


                                                                    There would be no problem. On the contrary, same ID exists in more than one place, the browser would be confused.






                                                                    share|improve this answer













                                                                    Usually, it is better not to use same ID multiple times in a html page.
                                                                    Even so, it is possible to use same ID many times in a page.
                                                                    However when you use an ID as a part of URI/URL as below:



                                                                    https://en.wikipedia.org/wiki/FIFA_World_Cup#2015_FIFA_corruption_case



                                                                    And if the ID('2015_FIFA_corruption_case') is used for only one (span)element in web page:



                                                                    <span class="mw-headline" id="2015_FIFA_corruption_case">2015 FIFA corruption case</span>


                                                                    There would be no problem. On the contrary, same ID exists in more than one place, the browser would be confused.







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Jan 1 at 2:01









                                                                    Park JongBumPark JongBum

                                                                    439713




                                                                    439713

















                                                                        protected by Mark Rotteveel Sep 6 '17 at 7:19



                                                                        Thank you for your interest in this question.
                                                                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                        Would you like to answer one of these unanswered questions instead?



                                                                        Popular posts from this blog

                                                                        Monofisismo

                                                                        Angular Downloading a file using contenturl with Basic Authentication

                                                                        Olmecas