I would like to put an onclick event on an area element. Here is my setup:
JavaScript
x
5
1
<img id="image" src="wheel.png" width="2795" height="2795" usemap="#Map" >
2
<map name="Map">
3
<area class="blue" onclick="myFunction()" shape="poly" coords="2318,480,1510,1284" href="#">
4
</map>
5
I have tried 2 different ways to have an onclick event. Firstly i tried this:
JavaScript
1
4
1
$(".blue").click( function(event){
2
alert('test');
3
});
4
I have also tried this:
JavaScript
1
4
1
function myFunction() {
2
alert('test');
3
}
4
Neither of the above work. Do area elements support the above, or do they only support having a href?
Advertisement
Answer
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:
JavaScript
1
7
1
$(".blue").on("click", function(e){
2
e.preventDefault();
3
/*
4
your code here
5
*/
6
});
7