HTML DOM Anchor Object

The <a> element is used in HTML to create hyperlinks along with href attribute. The anchor object represents this <a> element.


In the below example, we will learn how to access an anchor object −

 Live Demo

<!DOCTYPE html>
<h2>Demo Heading</h2>
<a href="" id = "myid" >Google</a><br><br>
<button onclick="display()">Display the link</button>
<p id="demo">Link gets displayed here</p>
   function display() {
      var a = document.getElementById("myid").href;
      document.getElementById("demo").innerHTML = a;


This will produce the following output −