Search code examples
javascripthtmlclickimagemap

Increasing variable count on imagemap click


I have an imagemap and wanted to keep track of how many times it had been clicked in javascript, but I have no idea how I would record the click count.

<p>
    <img src="image.png" alt="missing" width="396" height="376" border="0" usemap="#thatPageMap" />

    <map name="imageMap" id="thatPageMap">
        <area shape="poly" coords="0,376,-3,269,50,251,60,234,71,225,76,192,69,178,44,127,50,82,73,62,94,48,69,46,108,30,145,20,162,29,162,16,183,34,193,41,228,35,247,38,225,47,242,47,279,53,306,78,324,117,326,150,335,179,327,200,327,221,351,234,357,263,393,275,394,378" href="thatpage.com" alt="Refresh" />
    </map>
</p>

Above is the imagemap and below is the javascript and the paragraph with the counted ID.

<script type="text/javascript">

    var count;

    document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";

</script>

<p id="counted"></p>

How can I increase the variable count each time the imagemap is clicked?


Solution

  • For example like this:

    <script type="text/javascript">
        var count = 0;
        function updateCounter( ) {
            count++;  
            document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";
        }
    </script>
    
    <p id="counted"></p>
    
    <map name="imageMap" id="thatPageMap" onclick="updateCounter()">
        <area shape="poly" coords="0,376,-3,269,50,251,60,234,71,225,76,192,69,178,44,127,50,82,73,62,94,48,69,46,108,30,145,20,162,29,162,16,183,34,193,41,228,35,247,38,225,47,242,47,279,53,306,78,324,117,326,150,335,179,327,200,327,221,351,234,357,263,393,275,394,378" href="thatpage.com" alt="Refresh" />
    </map>