Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to fill color in a hotspot of an image map on click event.

I have an image map with 50 polygon hotspots and i want if i click on a perticular hotspot then area of that hotspot become gray color .........
Posted
Updated 18-Aug-20 6:50am

1 solution

Using JQuery you can get like this
HTML
<div style="width: 500px; height: 487px; overflow: auto; margin-removed 0px;">
                                <img src="Images/Minesweeper.png" width="793px" height="787px" class="map" alt="Parliament">
                                    usemap="#Parliamentmap" id="ImageMap1" />
                            </img></div>
                            <map id="map" name="Parliamentmap">
                                <area shape="poly" id="Area2" rel="0" coords="270,127,289,131,257,160,240,155" alt="ff" />
                                <area shape="poly" id="Area3" rel="1" coords="289,131,303,132,275,163,257,160" alt="bb" />
                                <area shape="poly" id="Area4" rel="1" coords="303,132,317,134,296,160,275,163" alt="bb" />
                                <area shape="poly" id="Area5" rel="1" coords="317,134,329,138,312,167,296,160" alt="tg" />
                                <area shape="poly" id="Area6" rel="1" coords="329,138,339,138,325,169,312,167" alt="hk" />
                                <area shape="poly" id="Area7" rel="1" coords="339,138,354,140,340,170,325,169" alt="ui" />
                                <area shape="poly" id="Area8" rel="1" coords="354,140,364,138,353,172,340,170" alt="oi" />
                                <area shape="poly" id="Area9" rel="1" coords="364,138,374,141,365,172,353,172" alt="ry" />
                                <area shape="poly" id="Area10" rel="1" coords="374,141,382,141,376,174,365,172" alt="iy" />
                                <area shape="poly" id="Area11" rel="1" coords="382,141,394,143,389,175,376,174" alt="sf" />
                                <area shape="poly" id="Area12" rel="1" coords="394,143,403,143,402,177,389,175" alt="rt" />
                                <area shape="poly" id="Area13" rel="1" coords="403,143,415,143,415,178,402,177" alt="mn" />
                            </map>

HTML
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
   <script src="Scripts/maphighlight.js" type="text/javascript"></script>

Note: You need to download these js files.
JavaScript
<script type="text/javascript">
        $(function () {
            $('area').click(function (event) {
                var map = document.getElementById('map');                
                var areas = map.getElementsByTagName('area');
                for (var i = 0; i < areas.length; i++) {
                    var area = areas[i];
                    var id = area.id;
                    var data = $('#' + id).data('maphilight') || {};
                    if (area.id == $(this)[0].id)
                        data.fillColor = '02FC1F'; // Sample color           
                    else data.fillColor = 'ff0000'; // Sample color           
                    $('#' + id).data('maphilight', data).trigger('alwaysOn.maphilight');
                }
            });
            $('.map').maphilight({ strokeColor: '808080', strokeWidth: 0, fill: 'ff0000', fillColor: 'ff0000', alwaysOn: true });
        });
    </script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900