tags:

views:

392

answers:

2

I want to know how to count the number of anchor tags present within a div element. e.g.:

<div>
<a href="1" >1</a>
<a href="2" >2</a>
<a href="3" >3</a>
<a href="4" >4</a>
</div>

How many <a> tags?

+16  A: 
theDivElement.getElementsByTagName('a').length
Fabien Ménager
simple is the best!
TheVillageIdiot
+4  A: 

Use HTML DOM getElementsByTagName() to get all "a" tags under an object.

To get the div you'de be better off giving it an ID:

<div id="thediv" >
    <a href="1">1</a>
</div>

and then use

var anchors = document.getElementById('thediv').getElementsByTagName('a');
Dror