Javascript DOM HTML Area href Property set

Introduction

The href property sets or gets the value of the href attribute of an area.

Possible values:

  • An absolute URL, href="http://www.example.com"
  • A relative URL, href="index.htm"
  • Anchor hash, href="#top"

Change the URL of a link in an area:

document.getElementById("myArea").href = "sun.htm";

Click the button to change the value of the href attribute.

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src="image1.png" width="100" height="100" usemap="#myFlagMap">

<map name="myFlagMap">
  <area id="myArea" 
        shape="circle" 
        coords="50,50,40" 
        alt="Target" 
        href="https://www.java2s.com">
</map>/*  w  w w .j  a  v  a  2 s  . c om*/

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  document.getElementById("myArea").href = "https://www.java2s.com/index.html";
  document.getElementById("demo").innerHTML = "The link now goes to index.htm.";
}
</script>

</body>
</html>



PreviousNext

Related