Javascript DOM HTML document links Collection

Introduction

Find out how many links there are in the document:

var x = document.links.length;

Click the button to display the number of links in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src ="image1.png" width="145" height="126" alt="Circle" usemap ="#myFlagMap">

<map name="myFlagMap">
  <area shape="rect" coords="0,0,30,30" href="https://java2s.com" alt="Site">
  <area shape="circle" coords="90,58,20" href="https://java2s.com" alt="Web">
  <area shape="circle" coords="50,50,40" href="https://www.java2s.com" alt="Target">
</map>//w  w  w  .j a v  a 2 s  . c o  m

<p>
  <a href="https://java2s.com">HTML</a><br>
  <a href="https://java2s.com">CSS</a>
</p>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.links.length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The links collection returns a collection of all links in the document.

The links in the collection represents <a> elements and/or <area> elements with a href attribute.

If the element is missing the href attribute, nothing is returned.

The elements in the collection are sorted as they appear in the source code.

Properties

Property Description
length Returns the number of <a> and/or <area> elements in the collection.

This property is read-only

Methods

Method
Description
[index]

Returns the <a> and/or <area> element from the collection with the index starting at 0.
Returns null if the index number is out of range
item(index)

Returns the <a> and/or <area> element from the collection with the index starting at 0.
Returns null if the index number is out of range
namedItem(id)

Returns the <a> and/or <area> element from the collection with the specified id.
Returns null if the id does not exist



PreviousNext

Related