StopPropagation with right click / right click

2

With this code I can stop the propagation of a dropdown menu by clicking on the document. It Works very well. Now I'd like to accomplish the same thing, but with the right click.

The magic happens thanks to event.stopPropagation(); within onclick what is the equivalent of this for the right click? Is it possible to use dual?

Functional code.

$(document).on('click','.body-jq-dropdown',function(event){ event.stopPropagation(); });

Example

The goal is that when the red background is enabled, you can remove it by clicking on the document.

$(document).on('click','.add-red-bg',function(event){
  jQuery('body').addClass('body-red-habilitado');
});
$(document).on('click contextmenu','.body-red-habilitado', function(event){
    event.stopPropagation();
    jQuery('body').removeClass('body-red-habilitado');
    console.clear();
    console.log(event.type);
});
body {
  background:#eee;
}

.body-red-habilitado {
  background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script><buttonclass="add-red-bg">Habilitar Background RED</button>
    
asked by anonymous 20.05.2018 / 01:30

1 answer

2

The contextmenu event means that the right button was clicked. Just add it to the event list of the .on() method.

But the problem is also that stopPropagation() should be applied to the button and not to document .

See:

$('.add-red-bg').on('click contextmenu',function(event){
   event.stopPropagation();
   $('body').addClass('body-red-habilitado');
});

$(document).on('click contextmenu', function(event){
    $('body').removeClass('body-red-habilitado');
    console.clear();
    console.log(event.type);
});
body {
  background:#eee;
}

.body-red-habilitado {
  background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonclass="add-red-bg">Habilitar Background RED</button>
    
20.05.2018 / 01:37