
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM links Collection
The links Collection returns a list/collection of all the links corresponding to <a> and/or <area> elements.
Syntax
Following is the syntax −
Returning links collection
document.links
Properties
Here, “links” collection can have the following properties and methods −
Property/Method | Description |
---|---|
length | It returns the number of <a> and <area> elements</a> |
namedItem() | It returns the <a> or/and <area> element with the specified id</a> |
Example
Let us see an example for links collection length property −
<!DOCTYPE html> <html> <head> <title>Links Collection length</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } area { border:1px solid black; } </style> </head> <body> <form> <fieldset> <legend>Links-Collection-length</legend> <a href="https://www.google.com">Google</a> <a href="https://www.bing.com">Bing</a> <input type="button" value="Get Links" onclick="getLinks()"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var extStyle = document.getElementById("extStyle"); function getLinks(){ var links = document.links.length; divDisplay.textContent = 'Total Links: '+links; for (var i = 0; i < links; i++) { divDisplay.textContent += '--> '+document.links[i]+' '; } } </script> </body> </html>
Output
This will produce the following output −
Before clicking ‘Get Links’ button −
After clicking ‘Get Links’ button −
- Related Articles
- HTML DOM Anchor Collection
- HTML DOM embeds collection
- HTML DOM scripts Collection
- HTML DOM forms collection
- HTML DOM Table rows Collection
- HTML DOM Table tBodies Collection
- HTML DOM Images Collection Property
- HTML DOM Datalist options Collection
- HTML Links
- How to make page links in HTML Page?
- HTML DOM HTML Object
- How to change the color of links in HTML?
- HTML DOM AnimationEvent
- HTML DOM DragEvent
- HTML DOM HashChangeEvent

Advertisements