suannai suannai - 5 months ago 8
Javascript Question

How to get < span > value?

There are a piece of code,but can not use

<id>
tag.

So,how do I get to the
<span> 1 2 3 4
?

<div id="test">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<div>

Answer

Try this

var div = document.getElementById("test");
var spans = div.getElementsByTagName("span");

for(i=0;i<spans.length;i++)
{
  alert(spans[i].innerHTML);
}