<div id="xxx"> <a href= .......></a> <a href=........></a> <a href=........></a> </div> How can we find out how many links inside of this <div>? I know document.links.length will give me the number of the all the links in the page. But I don't know how to count them inside a <div>. If there is a way, I would also like to know how to refer each individual link so I can modify it.
<html> <head> <script type="text/javascript"> function countLinks() { var container=document.getElementById("mydiv"); var linksInContainer=container.getElementsByTagName("a"); alert(linksInContainer.length);//alerts no of links in the container } </script> </head> <body> <div id="mydiv"> <a href="http://www.google.com">Google</a> <a href="http://www.yahoo.com">Yahoo</a> <a href="http://www.msn.com">MSN</a> </div> </body> </html> HTML: