views:

37

answers:

1

Hello,

<img src="" width="200" height="200" id="test_img" />

<script>
 $("#test_img").click(function() {
   alert("Hello");
 });
</script>

But it doesn't matter what mouse button I'm pressing - right or left. I see absolutely the same result. I want alert to be shown only on left mouse button click.

Thank you and sorry; I'm little new to javascript.

Thank you.

+4  A: 

You can evaluate the event.which attribute to determine which button has been pressed.

$("#test_img").mouseup(function(e) { 
  // e.which is 1, 2 or 3 for left / middle / right mouse button
  if (e.which === 1) {
    //continue
  }
}); 

Furthermore, to safely detect a mousebutton, you cannot use the click-event! Use mousedown or mouseup instead.

Try it out here!

Mef
Thank you for such detailed comment.
Kirzilla