Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App

HTML DOM links Collection

Last Updated : 09 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The DOM links collection is used to return the collection of all <a> and <area> elements with the "href" attribute in the HTML document. The elements in the collection are sorted as they appear in the sourcecode.

Syntax: 

document.links

Properties: It contains a single property length which is used to return the number of <a> and <area> elements in the collection.

Methods: The DOM links collection contains three methods which are listed below: 

  • [index]: It is used to return the <a> and <area> element of the specified index. The index value starts with 0. The NULL value is returned if the index value is out of range.
  • item(index): It is used to return the <a> and <area> element of selected index. The index value starts with 0. The NULL value is returned if the index value is out of range. This method performs similarly to the above method.
  • namedItem(id): It is used to return the <a> and <area> element from the collection which matches the specified id. NULL is returned if the id does not exist.

Return Value: An HTMLCollection Object, representing all <a> elements and/or <area> elements in the document. The elements in the collection are sorted as they appear in the source code

The below programs illustrate the use of the documents.links property in HTML:

Example 1: Using the length property to count the number of link elements in the collection. 

HTML
<!DOCTYPE html> <html> <head>     <title>         DOM document.links() Property     </title>     <!-- script to count links -->     <script>         function countLinks() {             let collection = document.links.length;             document.querySelector(".count").innerHTML =                 collection;         }     </script> </head> <body>     <h1>GeeksforGeeks</h1>     <h2>Articles:</h2>     <ul>         <!-- list of links -->         <li>             <a id="js"                 href= "https://www.geeksforgeeks.org/javascript-tutorial/">                 Javascript-Tutorial             </a>         </li>         <li>             <a id="python"                 href= "https://www.geeksforgeeks.org/python-programming-language/">                 Python Programming Language             </a>         </li>         <li>             <a id="cpp"                 href= "https://www.geeksforgeeks.org/c-plus-plus/">                 C++ Programming Language             </a>         </li>         <li>             <a id="html"                 href= "https://www.geeksforgeeks.org/html-tutorials/">                 HTML Tutorials             </a>         </li>     </ul>     <!-- button to count number of links -->     <button onclick="countLinks()">         Count links     </button>     <br>     <br>     <span>Total links: </span>     <span class="count"></span> </body> </html> 

Output: 

 

Example 2: HTML code to find all links in the document and return their IDs. 

HTML
<!DOCTYPE html> <html> <head>     <title>         DOM document.links() Property     </title>      <script>         /* function to find IDs */         function findLinkIDs() {             let final = '';             let collection = document.links;             // Run a for loop upto the number of             // links in the collection             for (let i = 0; i < collection.length; i++) {                 // Add each link id to a list                 final += `<li> ${collection[i].id} </li>`;             }             // Replace the HTML of the ID div             document.querySelector(".ids").innerHTML =                 final;         }     </script> </head> <body>     <h1>GeeksforGeeks</h1>     <h2>Articles:</h2>     <ul>         <!-- list of links -->         <li>             <a id="js"                 href= "https://www.geeksforgeeks.org/javascript-tutorial/">                 Javascript-Tutorial             </a>         </li>         <li>             <a id="python"                 href= "https://www.geeksforgeeks.org/python-programming-language/">                 Python Programming Language             </a>         </li>         <li>             <a id="cpp"                 href= "https://www.geeksforgeeks.org/c-plus-plus/">                 C++ Programming Language             </a>         </li>         <li>             <a id="html"                 href= "https://www.geeksforgeeks.org/html-tutorials/">                 HTML Tutorials             </a>         </li>     </ul>     <!-- button to find id -->     <button onclick="findLinkIDs()">         Find link IDs     </button>     <p>The IDs of all the links are: </p>     <div class="ids"></div> </body> </html> 

Output: 

 

Example 3: Using the id property to find by link ID and display its href attribute 

html
<!DOCTYPE html> <html> <head>     <title>         DOM document.links() Property     </title>     <script>         function returnLinkByID() {             let collection =                 document.links.namedItem("js");             // Get the href attribute             let link = collection.href;             document.querySelector(".name").innerHTML =                 link;         }     </script> </head>  <body>     <h1>GeeksforGeeks</h1>     <h2>Articles:</h2>     <ul>         <!-- collection of links -->         <li>             <a id="js"                 href= "https://www.geeksforgeeks.org/javascript-tutorial/">                 Javascript-Tutorial             </a>         </li>         <li>             <a id="python"                 href= "https://www.geeksforgeeks.org/python-programming-language/">                 Python Programming Language             </a>         </li>         <li>             <a id="cpp"                 href= "https://www.geeksforgeeks.org/c-plus-plus/">                 C++ Programming Language             </a>         </li>         <li>             <a id="html"                 href= "https://www.geeksforgeeks.org/html-tutorials/">                 HTML Tutorials             </a>         </li>     </ul>     <button onclick="returnLinkByID()">         Find by link ID     </button>     <p>         The href attribute in the link         with id of 'js' is:     </p>     <div class="name"></div> </body> </html> 

Output: 

 

Supported Browsers: The browser supported by DOM links collection method are listed below: 

  • Chrome 1 and above
  • Edge 12 and above
  • Internet Explorer 4 and above
  • Firefox 1 and above
  • Opera 12.1 and above
  • Safari 1 and above

S

sayantanm19
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • HTML
  • Technical Scripter 2018
  • Web technologies
  • HTML-DOM

Similar Reads

    HTML DOM scripts Collection
    The DOM scripts Collection in HTML is used to return the collection of all <script> elements in an HTML document. The script elements are sorted as appear in the sourcecode.  Syntax: document.scripts Property Value: It returns the single value length which is the collection of all script eleme
    2 min read
    HTML DOM getElementsByName() Method
    The getElementsByName() method returns collection of all elements of particular document by name. This collection is called node list and each element of the node list can be visited with the help of the index. Syntax: document.getElementsByName(name) Parameter:This function accepts name of document
    2 min read
    HTML DOM getElementById() Method
    The getElementById() method returns the elements that have given an ID which is passed to the function. This function is a widely used HTML DOM method in web designing to change the value of any particular element or get a particular element. If the passed ID to the function does not exist then it r
    3 min read
    HTML DOM getElementsByClassName() Method
    The getElementsByClassName() method in Javascript returns an object containing all the elements with the specified class names in the document as objects. Each element in the returned object can be accessed by its index. The index value will start with 0. This method can be called upon by any indivi
    3 min read
    HTML DOM defaultView Property
    The DOM defaultView property in HTML is used to return the document Window Object. The window object is the open windows in the browser. Syntax:document.defaultView Return Value: It is used to return the current Window Object. Example 1: In this example use the defaultView property to get the window
    2 min read
    HTML DOM forms Collection
    The DOM forms collection is used to return the collection of all <form> elements in a HTML document. The form elements are sorted as they appear in the source code. Syntax: document.forms Properties: It returns the number of form in the collection of elements. Methods: The DOM forms collection
    4 min read
    HTML DOM referrer Property
    The DOM referrer property in HTML is used to return the URI of the page that linked to the current page. If the user navigates to the page directly or through a bookmark, then this value is an empty string. Syntax:document.referrer The below program illustrates the referrer property in HTML: Example
    3 min read
    HTML DOM getNamedItem() Method
    The DOM getNamedItem() method in HTML is used to return the attribute node from the NamedNodeMap object.Syntax: namednodemap.getNamedItem( nodename )Parameter's Value: This method contains a single parameter nodename which is mandatory. The nodename is returned from the NamedNodeMap object. Return V
    2 min read
    HTML DOM createEvent() Event Method
    The createEvent() method in HTML creates an event object of the specified type. The created event must be initialized before use. Syntax:document.createEvent(event_type)event_type: It is the required parameter and is used to specify the type of event. There are many types of events which are listed
    2 min read
    HTML DOM getElementsByTagName() Method
    The getElementsByTagName() method in the HTML DOM allows the selection of elements by their tag name. It returns a collection of elements with the specified tag name within the specified document or element. To extract any info just iterate through all the elements using the length property. Syntax:
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences