Stoppropagation with right click / right click

Asked

Viewed 50 times

2

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

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

Functional code.

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

Example

The goal is, that when the red background is enabled, it is possible to remove it with a click 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>
<button class="add-red-bg">Habilitar Background RED</button>

1 answer

2


The event contextmenu means the right button has been clicked. Simply add it to the event list of the method .on().

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

Behold:

$('.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>
<button class="add-red-bg">Habilitar Background RED</button>

  • Works only with standard click. There is a button that when clicked adds on the body the class .body-jq-dropdown.

  • Didn’t work with the right click?

  • No, I know it’s working here... overall no, just the left, right or console will. I’ll add an example.

  • I put an example in the question.

  • remove the class that was added to the body by clicking on the body.

  • 1

    I updated the answer. See if this is it :D

  • Perfect.! Thx..

  • I think you can put the two events together so I don’t have to say it again

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.