getelementsbyclassname innerhtml code example

Example 1: js document.getelementsbyclassname modify innertext

<body>

<p class="demo">JavaScript can change the content of an HTML element.</p>    
<p class="demo">Yolo</p>   

<button type="button" onclick="myFunction()">Click Me!</button>

<script>        
function myFunction()
{
x=document.getElementsByClassName("demo");  // Find the elements
    for(var i = 0; i < x.length; i++){
    x[i].innerText="Hello JavaScript!";    // Change the content
    }

}

</script>
</body>

Example 2: return all class innerhtml in javascript

// It will retrun all innerHTML value into console.log
var elts = document.getElementsByClassName('nice-name');
for (var i = 0; i < elts.length; ++i) {
    console.log(elts[i].innerHTML);
}