Find out how many <a> elements there are in the document:
var x = document.anchors.length;
Click the button to display the number of anchors in the document.
<!DOCTYPE html> <html> <body> <a name="html">HTML Tutorial</a><br> <a name="css">CSS Tutorial</a><br> <a name="xml">XML Tutorial</a><br> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from ww w. j a v a2s . co m*/ var x = document.anchors.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The anchors collection returns a collection of all <a> elements in the document that have a name attribute.
The elements in the collection are sorted as they appear in the source code.
Property | Description |
---|---|
length | Returns the number of <a> elements in the collection. |
This property is read-only
Method | Description |
---|---|
[index] | Get the <a> element from the collection with the specified index (starts at 0). Returns null if the index number is out of range |
item(index) | Get the <a> element from the collection with the specified index (starts at 0). Returns null if the index number is out of range |
namedItem(id) | Get the <a> element from the collection with the specified id. Note: Returns null if the id does not exist |