Javascript DOM HTML Map Object get

Introduction

The Map object represents an HTML <map> element.

We can access a <map> element via document.getElementById():

var x = document.getElementById("myMap");

Click the button to get the number of area elements in the image-map.

View in separate window

<!DOCTYPE html>
<html>
<body>
<img src="image1.png" width="145" height="126" alt="Planets" usemap="#myMap">

<map id="myMap" name="myMap">
  <area shape="rect" coords="0,0,30,30"  alt="Web" href="https://www.java2s.com">
  <area shape="circle" coords="90,90,30" alt="Site" href="https://www.java2s.com">
  <area shape="circle" coords="10,10,20" alt="Target" href="https://www.java2s.com">
</map>//w  w  w  . ja v a2  s  .co  m
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.getElementById("myMap").areas.length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related