Javascript Reference - HTML DOM links Collection








The links collection returns an array of all the links in the current document.

Syntax

document.links[].property

Browser Support

links Yes Yes Yes Yes Yes

Example 1

The following code shows how to get the number of links in the document:


<!DOCTYPE html>
<html>
<body>
<p><a href="#">test</a></p>
<script>
document.write(document.links.length);
</script></p>
</body>
</html> 

The code above is rendered as follows:





Example 2

The following code shows how to get the id of the first link in the document.


<!DOCTYPE html>
<html>
<body>
<p><a id="javascript" href="#">test</a></p>
<!--  ww w.ja va  2 s .  c  o m-->
<p>Id of first area/link:
<script>
document.write(document.links[0].id);
</script></p>

</body>
</html> 

The code above is rendered as follows: