There is a div that I get by class name and which has a string as the innerHTML value. How do I get this string? If you write console.log(document.getElementsByClassName('player-track-info')); to the console console.log(document.getElementsByClassName('player-track-info')); then an object is displayed which has an innerHTML field of the following content

 innerHTML:"здСсь нСкий код" 

But if you type console.log(document.getElementsByClassName('player-track-info').innerHTML); then the console displays undefined. What am I doing wrong and how do I get the innerHTML value?

    1 answer 1

    There is a collection there. getElement s ByClassName .

    Take the innerHTML zero element if it is:

     document.getElementsByClassName('player-track-info')[0].innerHTML 

    And here is a working example:

     console.log( document.getElementsByClassName('myclass') ); console.log( document.getElementsByClassName('myclass')[0] ); console.log( document.getElementsByClassName('myclass')[0].innerHTML ); 
     <div class="myclass"> <span class="someclass">ΠΊΠ°ΠΊΠΎΠ΅-Ρ‚ΠΎ содСрТимоС</span> </div> 

    • There is no such zero element as it gives an error ... - vas
    • one
      So the question is incorrect, because according to the data specified in it (namely, the first console.log), this code will definitely work - andreymal
    • I added the code on the fiddle, the moderators will now load. - Sergey Snegirev
    • @SergeySnegirev, I corrected your addition, in the SO engine, fiddles can be embedded directly in response - ixSci
    • <div class = "player-track-info"> 3 <span class = "text_theme"> / </ span> 38. Get Out Your Self <span class = "text_subtle"> by </ span> <a target = " _blank "href =" site.ru/">Shekinah</a> </ div> </ div> @Qwertiy: this is the code in html. Its getting document.getElementsByClassName ('player-track-info') ;. The console shows that this is an HTMLCollection [] and that's it .. What could it be? Can not get to the elements. - vas