JavaScript document.anchors Property
You are Here:
JavaScript document.anchors Property
The document.anchors
property returns the data related to the anchors in the current document.
Note: This property is read-only.
Warning: The document.anchors
property is deprecated and is no longer recommended.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<a name="JavaScript" href="/js">Learn JavaScript</a><br><br>
<a name="jQuery" href="/jquery">Learn jQuery</a>
<p>Click the button</p>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
function myFunction(){
x.innerHTML = document.anchors.length
}
</script>
</body>
</html>
Syntax
document.anchors
Return Values
Value | Explanation |
---|---|
Object | Returns the data related to the anchors in the current document. |
Get the Text of the Anchor tag at zero index (Using Square Bracket)
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<a name="JavaScript" href="/js">Learn JavaScript</a><br><br>
<a name="jQuery" href="/jquery">Learn jQuery</a>
<p>Click the button</p>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
function myFunction(){
x.innerHTML = document.anchors[0].innerText
}
</script>
</body>
</html>
Get the Text of the Anchor tag at zero index (Using Round Bracket)
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<a name="JavaScript" href="/js">Learn JavaScript</a><br><br>
<a name="jQuery" href="/jquery">Learn jQuery</a>
<p>Click the button</p>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
function myFunction(){
x.innerHTML = document.anchors.item(0).innerText
}
</script>
</body>
</html>
Get the Link of the Anchor tag at zero index
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<a name="JavaScript" id="myJS" href="/js">Learn JavaScript</a><br><br>
<a name="jQuery" id="myjQuery" href="/jquery">Learn jQuery</a>
<p>Click on the button</p>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
function myFunction(){
x.innerHTML = document.anchors[0].href;
}
</script>
</body>
</html>
Get the Link of the Anchor tag using ID Attribute
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<a name="JavaScript" id="myJS" href="/js">Learn JavaScript</a><br><br>
<a name="jQuery" id="myjQuery" href="/jquery">Learn jQuery</a>
<p>Click on the button</p>
<button onclick="myFunction()">Click Me</button>
<p id="point"></p>
<script>
var x = document.getElementById("point");
function myFunction(){
x.innerHTML = document.anchors.namedItem("myJS").href;
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.