How to find the second id of something

2

How can I get access to the second div with the id general as I tried in the script?

<div id="general">ds</div>
<div id="general">ds</div>'

alert($("#general").eq(2).html());
    
asked by anonymous 09.12.2014 / 19:32

1 answer

7

This is not possible * because IDs must be unique . If you need to identify more than one element with a name, use classes instead of IDs:

<div class="general">ds</div>
<div class="general">ds</div>
var divs = document.querySelectorAll('.general');
alert(divs[0].innerHTML);
alert(divs[1].innerHTML);

* In fact it is possible, as other answers and comments show. But you should never repeat the ID in HTML, and so I do not want to show you the hassles that allow you to select those elements with repeated IDs.

    
09.12.2014 / 19:33