I would like to put an onclick event on an area element. Here is my setup:
<img id="image" src="wheel.png" width="2795" height="2795" usemap="#Map" >
<map name="Map">
<area class="blue" onclick="myFunction()" shape="poly" coords="2318,480,1510,1284" href="#">
</map>
I have tried 2 different ways to have an onclick event. Firstly i tried this:
$(".blue").click( function(event){
alert('test');
});
I have also tried this:
function myFunction() {
alert('test');
}
Neither of the above work. Do area elements support the above, or do they only support having a href?
Thanks in advance!
Pay attention:
Attribute href is obligatory, without it the area-tag does nothing!
To add a click event, you'll need to block default href.
Your code should start as follows:
Live example here.
Based on your comments, you just need this:
Demo:
http://codepen.io/tuga/pen/waBQBZ
Use a class for all elements you want to listen on, and optionally an attribute for behavior:
Then add your event listeners to all elements in the class:
Demo: https://codepen.io/weird_error/pen/xXPNOK
This is a simple one:
Try :
You souldn't want to add
onClick
events on area, documentation :Edit : your coords are a bit weird since its supposed to the couples of each vertices (so right now, your polygon is a line)
Its the shape that's the problem. Your code has set shape equal to polygon but only has 4 points in the coordinates attribute. You need to set shape to rectangle instead.
Set shape="rect" like this:
<img id="image" src="wheel.png" width="2795" height="2795" usemap="#Map" > <map name="Map"> <area class="blue" onclick="myFunction()" shape="rect" coords="2318,480,1510,1284" href="#"> </map>