

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a particular anchor in a document in JavaScript?
Javascript anchor tags follow an array-like structure. When we try to display a particular anchor tag we have to use document.anchors.innerHTML method. This method works the same as array methods which are used to display a particular element.
syntax
element = document.anchors[i].innerHTML;
Example-1
In the following example, the first anchor tag, out of 2 anchor tags, is displayed as shown in the output.
<html> <body> <a name="Tutor">Tutorix</a><br> <a name="Totorial">Tutorialspoint</a><br> <p id="anchors"></p> <script> document.getElementById("anchors").innerHTML = "The first anchor element is: " + document.anchors[0].innerHTML; </script> </body> </html>
Output
Tutorix
Example-2
In the following example, there are 3 anchor elements in which the third element is displayed using document.innerHTML as shown in the output.
<html> <body> <a name="Tutor">Tutorix</a><br> <a name="Totorial">Tutorialspoint</a><br> <a name="Totorials">Tutorialspoint private ltd</a><br> <p id="anchors"></p> <script> document.getElementById("anchors").innerHTML = "The third element is:" + document.anchors[2].length; </script> </body> </html>
Output
Tutorix Tutorialspoint Tutorialspoint private ltd The third element is:Tutorialspoint private ltd
- Related Questions & Answers
- How to delete particular data in a document in MongoDB?
- How to get the cookies associated with a document in JavaScript?
- How to reach subdata in MongoDB and display a particular document?
- How to get the number of forms in a document with JavaScript?
- How to get the entire document HTML as a string in JavaScript?
- How to get a particular date in Java 8?
- How to get embedded data in a MongoDB document?
- How to get the title and full URL of a document in JavaScript?
- How to get a particular element from MongoDB array?
- How to find a number of anchors in a document in JavaScript?
- How to write Python regular expression to get all the anchor tags in a webpage?
- MongoDB query to fetch a document that does not have a particular field?
- How to delete a particular element from a JavaScript array?
- How to check if a document is ready in JavaScript?
- How to get a timestamp in JavaScript?
Advertisements