HTML DOM Anchor href Property

The HTML DOM Anchor href property is used to set or return the href attribute. It returns the url of the link.

Following is the syntax to set the anchor href property−

anchorObj.href = URL

Above, URL is the url of the link. It can be an absolute, relative or anchor link with a hash.

Following is the syntax to return the anchor href property−


Let us now see an example to implement the DOM Anchor href property−


 Live Demo

<!DOCTYPE html>
<p><a id="mylink" href="">Our Team</a></p>
<h2 id="myid"></h2>
<button onclick="display()">Display href Part</button>
<button onclick="display2()">Display Host Part</button>
<button onclick="display3()">Display Hostname</button>
function display() {
   var a = document.getElementById("mylink").href;
      document.getElementById("myid").innerHTML = a;
   function display2() {
      var a = document.getElementById("mylink").host;
      document.getElementById("myid").innerHTML = a;
   function display3() {
      var a = document.getElementById("mylink").hostname;
      document.getElementById("myid").innerHTML = a;


Click on the “Display href Part” to display the url −

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started