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.
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.
1 Answer
Reset to default 15Since 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.
innerHTML
is exactly what you want to get? Have you had a look at some documentation? What have you tried so far?document.getElementsByTagName
is a good start, what exactly are you having problems with now? Obviously we won't do your homework... – Felix Kling Commented Feb 27, 2012 at 16:51