The Anchor object represents an HTML <a> element.
We can access an <a> element by using getElementById()
:
var x = document.getElementById("myAnchor");
Click the button to get the URL of the link.
<!DOCTYPE html> <html> <body> <p id="myResult"></p> <button onclick="myFunction()">Test</button> <a id="myAnchor" href="https://www.java2s.com">Tutorials</a> <script> function myFunction() {//www. j a va2 s . c o m var x = document.getElementById("myAnchor").href; document.getElementById("myResult").innerHTML = x; } </script> </html>