views:

645

answers:

3

Using the event click with live function leads to strange behavior when using Firefox*. With live in Firefox, click is triggered when right-clicking also! The same does not happen in Internet Explorer 7 neither in Google Chrome.

Example:

  • Without live, go to demo and try right clicking the paragraphs. A dialog menu should appear.
  • With live, go to demo and try right clicking "Click me!". Now both dialog menu and "Another paragraph" appear.

*tested with firefox 3.5.3

+1  A: 

I think it's a known "bug", you could potentially query the event object after attaching the click handler ( which gets attached to the document ) and see if its a right click, otherwise manually attach the click handler after you manipulate the DOM.

After looking it up, e.button is the property you want to query:

.live('click', function(e){

if ( e.button == 2 ) return false; // exit if right clicking

// normal action

});
meder
+4  A: 

As far as I know, that is a known issue (bug?). You can easily work around it by testing which button was clicked as follows:

$('a.foo').live("click", function(e) { 
    if (e.button == 0) { // 0 = left, 1 = middle, 2 = right 
        //left button was clicked
    } else {
        //other button was clicked (do nothing?)
        //return false or e.preventDefault()
    }
});

you might prefer using a switch depending on your specific requirements, but generally you would probably just want to do nothing (or or simply return) if any button other than the left button is clicked, as above:

    $('a.foo').live("click", function(e) {
        switch(e.button) {
            case 0 : alert('Left button was clicked');break;
            default: return false;
        }
    });
karim79
+1  A: 

See my answer here: if you don't mind changing the jQuery source a bit, adding a single line in the liveHandler() works around the problem entirely.

nickf