Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  33] [ 6]  / answers: 1 / hits: 67531  / 9 Years ago, tue, april 28, 2015, 12:00:00

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?


More From » jquery

 Answers
10

Pay attention:




  1. Attribute href is obligatory, without it the area-tag does nothing!


  2. To add a click event, you'll need to block default href.







Your code should start as follows:



$(.blue).on(click, function(e){
e.preventDefault();
/*
your code here
*/
});


Live example here.


[#66853] Sunday, April 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
montana

Total Points: 675
Total Questions: 86
Total Answers: 102

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
;