HTML DOM Map Object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Map Object in HTML represents the <map> element.

Syntax

Following is the syntax −

Creating a <map> element

var mapObject = document.createElement(“MAP”)

Properties

Here, “mapObject” can have the following collections & properties −

Collection/ PropertyDescription
areasIt returns a collection of all <area> elements
imagesIt returns a collection of all <img> & <object> elements
nameIt sets/returns the value of the name attribute for <map> element

Example

Let us see an example for Map areas collection −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Map areas collection</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>Map-areas-collection</legend>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/New7Wonders.jpg/276px-New7Wonders.jpg" width="250" height="150" usemap="#7Wonders">
<map id="WonderWorld" name="7Wonders">
</map>
<div id="divDisplay"></div>
<input type="button" value="Apply Link" onclick="myWonder()">
</fieldset>
</form>
<script>
   function myWonder() {
      var divDisplay = document.getElementById("divDisplay");
      var newArea = document.createElement("AREA");
      newArea.setAttribute("href", "https://en.wikipedia.org/wiki/Giza_pyramid_complex");
      newArea.setAttribute("shape", "rect");
      newArea.setAttribute("coords", "120,10,10,40");
      document.getElementById("WonderWorld").appendChild(newArea);
      divDisplay.textContent = "Link was applied, hover and click on pyramid of giza.";
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Apply Link’ button −

After clicking ‘Apply Link’ button −

raja
Published on 31-Jul-2019 14:04:54
Advertisements