HTML | DOM Map Object

The Map Object in HTML DOM is used to create and access the <map> element. The Map Object create the portion of image clickable and accessible.

Syntax:

  • It is used to return Map object.
    var x = document.getElementById("myMap");
  • It is used to create Map Object.
    var x = document.createElement("MAP");

Property Values:



  • name: It is used to set or return the value of the name attribute of an image-map.

Collection Values:

  • areas: It is used to return the collection of all <area> elements in an image-map.
  • images: It is used to returns the collection of all <img> and <object> elements associated with the image-map

Example 1: This example describes the getElementById() method to access the <map> element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title
        HTML DOM Map Object Property 
    </title>
</head>
  
<body>
      
    <h4>Click the button</h4>
      
    <button onclick = "GFG()">
        Click Here!
    </button>
      
    <p></p>
      
    <map id = "Geeks" name = "Geeks">
          
        <area  = "rect" coords = "0, 0, 110, 100"
        alt = "Geeks" href
  
        <area shape = "rect" coords = "110, 0, 190, 100"
        alt = "For" href =
  
        <area shape = "rect" coords = "190, 0, 300, 100"
        alt = "GEEKS" href =
    </map>
      
    <img src =
    width = "300" height = "100" alt="GFG" usemap = "#Geeks">
      
    <p id = "GEEK!"></p>
      
    <script>
        function GFG() {
            var x = document.getElementById("Geeks").areas.length;
            document.getElementById("GEEK!").innerHTML = x;
        }
    </script>
</body
  
</html>                    

chevron_right


Output:

Example 2: This example describes the document.createElement() method to create <map> element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title
        HTML DOM Map Object Property 
    </title>
</head>
      
<body>
    <h4>Click the button</h4>
      
    <button onclick = "GFG()">
        Click Here!
    </button>
      
    <p></p>
      
    <img src
    width = "300" height = "100" usemap = "#myMap">
      
    <p id = "GEEK!"></p>
      
    <!-- script to use map object property -->
    <script>
        function GFG() {
            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", 
            y.setAttribute("shape", "rect");
            y.setAttribute("coords", "190, 0, 300, 100");
            document.getElementById("myMap").appendChild(y);
  
            document.getElementById("GEEK!").innerHTML = 
            "Click on the GEEKS area in the image.";
        }     
    </script>
</body>
  
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by DOM Map Object property are listed below:

  • Google Chrome 5.0
  • Internet Explorer 8.0
  • Firefox 3.6
  • Safari 5.0
  • Opera 10.6


My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.