I am trying to add a div inside a nested divs structure in one of my project.
below is the sample I wrote to replicate the behavior. what could possibly be going wrong with it that I am not able to see widget div after clicking the button?
function myFunction() {
var widget = document.createElement("div");
widget.id = 'widget';
widget.innerHTML = "I am Widget";
var x = document.getElementsByClassName("model-viewer");
x[0].appendChild(widget);
console.log(x)
x[0].innerHTML = "Hello World!";
}
<div class="example">First div element with class="example".
<div class="app_main">
<div class="model"></div>
<div class="model-filter">
<div class="model-viewer">hello</div>
</div>
<div class="mod"></div>
</div>
</div>
<button onclick="myFunction()">Try it</button>
<p><strong>Note:</strong> The getElementsByClassName() method is not supported in Internet Explorer 8 and earlier versions.</p>