3
....live('click', function(){
   /*How do I distinguish left/right/double click*/
});

It seems event.button can be used to distinguish left and right click, but how to distinguish double click?

And I don't know if event.button is supported by all major browsers..

Je Rog
  • 5,675
  • 8
  • 39
  • 47
  • 4
    As far as I know, you must use `dblclick` handler for that, not `click`... – Cipi Sep 26 '11 at 13:00
  • See this for possible answer to your question: [How to distinguish between left and right mouse click with jQuery?](http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery) – Shadow The GPT Wizard Sep 26 '11 at 13:09

2 Answers2

4

This seems to solve the left and right click issue:

$("#element").live('click', function(e) {
    if( e.button == 0 ) {
        // Left mouse button was clicked (non-IE)
    }
});

For IE

$("#element").live('click', function(e) {
    if( e.button == 1 ) {
        // Left mouse button was clicked (IE only)
    }
});

See the link for more details

jQuery live click binds

cillierscharl
  • 7,043
  • 3
  • 29
  • 47
Prasanth
  • 3,029
  • 31
  • 44
3

well there is a separate event handler for doubleclick.. The event is dblclick

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • But I need to use it with `live` to bind future elements. – Je Rog Sep 26 '11 at 13:05
  • The `dblclick` handler will also fire the `click` handler, at least in certain operating systems and/or browsers. It is "officially" inadvisable to use both. I suppose one could apply a time delay to the contents of the `click` handler and then test if the `dblclick` event was fired. – Blazemonger Sep 26 '11 at 13:16
  • What about left/right click? Is there a more elegant solution than @Prasanth's ? – Je Rog Sep 26 '11 at 13:19
  • @JeRog: the `click` handler only detects a mouse click, not which button. That's as elegant as it gets. – Blazemonger Sep 26 '11 at 13:22