HTML DOM links Collection
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.
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.
Example 2: HTML code to find all links in the document and return their IDs.
Example 3: Using the id property to find by link ID and display its href attribute
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
Please Login to comment...