Yesterday, I stumbled upon a stumbling block in JS , and it turned out to be impossible to write against the wind.

Actually there is a page (not mine), in my case with two identical id .. sadness .. But still with the first id - javascript works. Is it possible in some way with the help of js / jq to change the id itself to the first necessary element? and then with the help of JS to address both to it, on a new id , and to that, the second element, with the same id ?

I know, many people don’t like the question, want 2m elements to make a new class

 function(){document.getElementById('my_class').className += 'hide_my';} 


    1 answer 1

     <b id="x">123</b> <b id="x">321</b> <script> alert(document.getElementById('x').innerHTML); document.getElementById('x').id = 'x2'; alert(document.getElementById('x2').innerHTML); alert(document.getElementById('x').innerHTML); </script> 
    1. prints 123
    2. prints 123
    3. displays 321

    Variant2 which can also be useful to you:

     <b id="x">123</b> <b id="x">321</b> <script> list1 = document.getElementsByTagName('b'); for (var i=0; i<list1.length; i++) { if (list1[i].id == "x") { alert(list1[i].innerHTML); } } </script> 

    here you can already get to all elements with the same id :)

    • one
      <div id = 'element'> </ div> <div id = 'element'> </ div> <script> var elem1 = document.getElementById ('element'); elem1.setAttribute ('id', elem1.getAttribute ('id') + Math.random ()); var elem2 = document.getElementById ('element'); elem1.innerHTML = '1'; elem2.innerHTML = '2'; </ script> - lampa
    • oh, read something inattentively) - IVsevolod