How to get the value of the usemap attribute of an image with JavaScript?

To get the value of the usemap attribute of an image, use the useMap property. This property returns the value of the usemap attribute, which specifies which image map to use with the image.

Syntax

let value = imageElement.useMap;

Example

<!DOCTYPE html>
<html>
   <body>
      <img id="myid" src="/images/html.gif" alt="HTML Map" usemap="#html"/>
      <map name="html">
         <area id="myarea" shape="circle" coords="154,150,59" href="/about.htm" alt="Team">
      </map>
      
      <script>
         var x = document.getElementById("myid").useMap;
         document.getElementById("output").innerHTML = "Usemap: " + x;
      </script>
      
      <p id="output"></p>
   </body>
</html>

Output

Usemap: #html

How It Works

The useMap property accesses the usemap attribute value of an image element. The usemap attribute creates a relationship between the image and a map element, allowing you to define clickable areas on the image.

In the example above:

  • The image has usemap="#html"
  • The useMap property returns the string "#html"
  • This corresponds to the map with name="html"

Conclusion

The useMap property provides a simple way to retrieve the usemap attribute value of an image element. This is useful when working with image maps and need to programmatically access the map reference.

Updated on: 2026-03-15T23:18:59+05:30

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements