As in JavaScript, knowing the element class to get its id

<span id="idelement" class="classelement">ТЕСТ</span> 

I tried it this way but here I get the opposite class and I need an id

 var result = window.content.document.getElementById("idelement").className; alert(result); 
  • See getElementsByClassName () . Of course, you get a collection of all the elements with this class. - PinkTux
  • @PinkTux So you need it? I'm not very Java. var result = document.getElementsByClassName ("classelement"). getElementById (); Recorded wrong, already understood. But how to get an id knowing his class I didn’t understand vseravno - Anatoly
  • JavaScript is not java! See the answer. - PinkTux

1 answer 1

See getElementsByClassName () . The output will be a collection of elements, since, unlike id , the class of an element is not unique, and there may be as many elements with a given class as a document. Using:

 var elements = document.getElementsByClassName("myclass"); for( var i = 0; i < elements.length; i++ ) { alert( elements[i].id ); } 
  • It works, thank you very much. - Anatoly