Javascript DOM HTML Map Object create

Introduction

We can create a <map> element via the document.createElement() method:

var x = document.createElement("MAP");

Click the button to create an IMAGE-MAP and an AREA element with a link to "https://www.java2s.com".

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src="image1.png" width="100" height="100" usemap="#myMap">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {//from  w  w w. j  a v  a2 s.co m
  var x = document.createElement("MAP");
  x.setAttribute("id", "myMap");
  x.setAttribute("name", "myMap");
  document.body.appendChild(x);

  var y = document.createElement("AREA");
  y.setAttribute("href", "https://www.java2s.com");
  y.setAttribute("shape", "circle");
  y.setAttribute("coords", "50,50,40");
  document.getElementById("myMap").appendChild(y);

  document.getElementById("demo").innerHTML = "The MAP was created, and you can now click on the area in the image.";
}
</script>

</body>
</html>



PreviousNext

Related