To get all the anchor tags or links from the HTML document or the DOM, you can use the document.links property.
Let's say you have a list of many anchor tags all around your HTML document like this,
<div>
<h1>Heading 1</h1>
<a href="https://google.com">Read More</a>
</div>
<div>
<h1>Heading 2</h1>
<a href="https://google.com">Read More</a>
</div>
<div>
<h1>Heading 3</h1>
<a href="https://google.com">Read More</a>
</div>
- You can get all the links using the
document.linksproperty using JavaScript.
// get all links
const listOfLinks = document.links;
console.dir(listOfLinks);
- The
document.linksproperty returns the list of links as anHTMLCollection.
To see the working example see JSBin.