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");
for(var i = 0; i < x.length; i++){
x[i].innerText="Hello JavaScript!";
}
}
</script>
</body>
Example 2: return all class innerhtml in javascript
var elts = document.getElementsByClassName('nice-name');
for (var i = 0; i < elts.length; ++i) {
console.log(elts[i].innerHTML);
}