0

Here is my code:

$('a').on('click', function(){
  myfunc($(this));
});

function myfunc(el){
  console.log('Either left or middle click clicked on the link');
}
a{
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a>click</a>

My code works just for left click. How do I run that function when the users clicks on the link through the middle-click too?

stack
  • 10,280
  • 19
  • 65
  • 117

2 Answers2

0
$("a").on("mousedown", function(e){
    switch(e.which)
    {
        case 1:
            //left Click
        break;
        case 2:
            //middle Click
        break;
    }
    return true;
});
0

Try this:

$('a').on('mousedown', function(e){
    if( e.which <= 2 ) {
        myfunc($(this));
    } 
});

function myfunc(el){
  console.log('Either left or middle click clicked on the link');
}
stack
  • 10,280
  • 19
  • 65
  • 117