JQuery click () event; how to prevent this event on the right mouse button?

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

      

But it doesn't matter which mouse button I press - right or left. I see exactly the same result. I want the alert to only show when left clicking.

Thanks and sorry; I am a bit new to javascript.

Thanks.

+2


a source to share


1 answer


You can evaluate the attribute event.which

to determine which button was clicked.

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

      



Also, to safely detect the mouse button, you cannot use click

-event! Use mousedown

or instead mouseup

.

Try it here!

+4


a source







All Articles