Anchor href Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor


The href property sets or gets the value of the href attribute of a link.

Property Values

Value Description
URL Sets the URL of the link.

Possible values for href property:

  • An absolute URL - points to another web site, like href=""
  • A relative URL - points to a file within a web site, like href="default.htm"
  • An anchor URL - points to an anchor within a page, like href="#top" or href=""

Return Value

A String, representing the entire URL of the link, including the protocol, like http://

The following code shows how to Change the destination (URL) of a link:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<p><a id="myAnchor" href=""></a></p>

<button onclick="myFunction()">change the value of the href attribute of the link</button>

<p id="demo"></p>

function myFunction() {/*ww  w . j  a  va2  s .c o  m*/
    document.getElementById("myAnchor").href = "";
    document.getElementById("demo").innerHTML = "The link above now goes to";


Related Tutorials