lundi 26 juillet 2021

How does this JavaScript code not working with getElementsByClass? [duplicate]

I'm trying to find an element by classname and print some elements after a user presses a button. The texts I want to print, are inside an array of the method I'm calling.

Why does this code below, not print the text in that element?

<html>
        <script>
            function perform(){
                let fruits= ['apple','mango','kiwi','jackfruit'];
                document.getElementsByClassName('king').innerHTML= fruits[1];
            }
        </script>
    
    <body>
        <p class='king'>inflate me</p>
        <button onclick='perform()'>click to see</button>
        
    </body>
</html>



Aucun commentaire:

Enregistrer un commentaire