I need an idea on how to start a loop where I can get the innerHTML for the 3 divs inside a div:
<div id="hi">
<div> Item1 </div>
<div> Item2 </div>
<div> Item3 </div>
</div>
I need to make a function that search through the item list and see for common items. I know one way is to use document.getElementsByTagName
but I don't need to see the innerHTML for each div.
Since getElementsByTagName()
returns an array, you can use a for
loop for each of the elements.
var div = document.getElementById('hi');
var divs = div.getElementsByTagName('div');
var divArray = [];
for (var i = 0; i < divs.length; i += 1) {
divArray.push(divs[i].innerHTML);
}
This will push the innerHTML
of each of the elements into the divArray
variable and iterate through them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With