0


i need i nice solution to manage the left-& rightclick on a div with a special class (live).
It is important for me that the code works on every new browser.

Hope somebody can help me.


My first try ... // the rightclick does not work

HTML

<div id="test_one" class="my_class">Click here</div>
<br /><br />
<div id="test_two" class="my_class">Click here</div>

JS

$(document).ready(function test()
{

    $(".my_class").bind("contextmenu",function(e){ return false; });

    $(".my_class").live('click', function(e)
    {
      if(e.button == 0 || e.button == 1)
      {
           alert('L -> '+this.id+'');
      }
      else if(e.button == 2){
           alert('R -> '+this.id+'');
      }
    });


});

example: http://jsfiddle.net/EWXse/

Thanks in advance! Peter

Peter
  • 11,413
  • 31
  • 100
  • 152
  • possible duplicate of http://stackoverflow.com/questions/1206203/how-to-distinguish-left-mouse-click-and-right-with-jquery – Alpesh Jan 11 '11 at 10:21
  • even this link contains the same question http://stackoverflow.com/questions/4007482/jquery-detect-if-middle-or-right-mouse-button-is-clicked-if-so-do-this – Alpesh Jan 11 '11 at 10:25

1 Answers1

1

According to the documentation of mousedown the right mouse lick is not detectable by default: http://api.jquery.com/mousedown/ .

How to distinguish between left and right mouse click with jQuery suggests that you use event.which instead of event.button, as .which is normalised to 1,2,3 across all browsers.

For what it is worth: right mouse clicks don't seem to work in Google Chrome on Mac OS X here.

Community
  • 1
  • 1
Coroos
  • 371
  • 2
  • 9