jQuery Event How to - Detect middle mouse button click event








Question

We would like to know how to detect middle mouse button click event.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(function(){<!--from w w  w .j av  a  2 s. co m-->
    $(document).on("mousedown", "a.external", function(e) {
       console.log(e.which);
       if( e.which <= 2 ) {
          e.preventDefault();
          console.log ("Button clicked: " + e.which);
       }else{
          console.log ("Button clicked else: " + e.which);
       }
       
    });
});
</script>
</head>
<body>
  <a class="external">Test</a>
</body>
</html>

The code above is rendered as follows: