getElementsByTagName
returns a collection of all the matching elements(<div>
s in this case) on the page/DOM, to select first element use array notation with zero index.
document.addEventListener("DOMContentLoaded", function() {
var text = "Planing";
document.getElementsByTagName("div")[0].innerHTML = text;
});
<div id="demo" class="eg"></div>
If you want to select first element, you can use document.querySelector('div');
If you want to perform some operation on all the selected elements, you need to iterate over them.
var allDivs = document.getElementsByTagName("div");
for (var i = 0; i < allDivs.length; i++) {
allDivs[i].innerHTML = 'Div ' + i;
}