3

I want to count klicks on links with jQuery. It works fine if the links are clicked with left mouse button. But it should also work if the right button is used. Here is the code:

<a href="http://example.com" class="itemLink" data-count-url="/239/klicks">

$(".itemLink").on("click", function(event) {
   $.ajax({
     type: 'POST',
     url: $(event.target).attr("data-count-url")
   })
});

As far as I know click should work with the left and right button. What is wrong with my code?

deamon
  • 89,107
  • 111
  • 320
  • 448

1 Answers1

4

Use mousedown event instead of click event and implement all needed behaviors

How to distinguish between left and right mouse click with jQuery

$('.itemLink').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left mouse button pressed');
            break;
        case 2:
            alert('Middle mouse button pressed');
            break;
        case 3:
            alert('Right mouse button pressed');
            break;
        default:
            alert('You have a strange mouse');
    }
});
Community
  • 1
  • 1
sdespont
  • 13,915
  • 9
  • 56
  • 97