From my understanding, document.querySelector
returns a Node
object. I can then call appendChild
on this object.
I execute the following code to append a bunch of divs to my container div:
var container = document.querySelector('.container');
for (var i = 0; i < 400; i++) {
var block = document.createElement('div');
block.className = 'block';
container.appendChild(block);
}
And end up with the following structure:
<div class="container">
<div class="block"></div>
<div class="block"></div>
...
<div class="block"></div>
</div>
How can I loop through each element in my container div and add a new class to it using my existing container
variable?
I have tried this:
...
container.childNodes[i].className = 'myClass';
It seems I need to access the Element
object of the child Node
, but I'm not sure how to do this.