Sadesh Kumar N Sadesh Kumar N - 4 months ago 18
Javascript Question

Triggering onclick event using middle click

I am using the

onclick
event of a hashed link to open a
<div>
as a pop up. But the middle click does not trigger the
onclick
event
but only takes the
href
attribute value of the link and loads the URL in a new page. How can I use middle click to open the
<div>
as a popup?

Answer

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").live('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
      alert("middle button"); 
   }
});

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

Comments