Triggering onclick event using middle click

EDIT

This answer has been deprecated and doesn’t work on Chrome. You will most probably end up using the auxclick event, but please refer to other answers below.

/EDIT


beggs’ answer is correct, but it sounds like you want to prevent the default action of the middle click. In which case, include the following

$("#foo").on('click', function(e) {
   if (e.which == 2) {
      e.preventDefault();
      alert("middle button"); 
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="foo" href="http://example.com">middle click me</a>

preventDefault() will stop the default action of the event.

Leave a Comment