How to cancel event click on site

Asked

Viewed 675 times

2

I use a plugin jQuery which generates a hidden side menu and when clicking the button, the menu appears, shifting the content of the site to the side. The problem is that the site elements remain "clickable" and the menu does not close automatically if I click outside it.

With that, I created the script below:

$('.wrapp').on('click', function(){
    if ( ($('#sidr').is(':visible')) ) {
        $(this).click(false);
        $.sidr('close', 'sidr');
    }
});

At first, it should run when clicked on any element within the div .wrapp, closing the menu (id #sidr) and overriding the possible alternative click.

Although the menu is closing right, I can still click on any link outside the menu.

Best explanation in the picture: inserir a descrição da imagem aqui

  • You can create a jsFiddle with an example?

  • Difficult, because the menu requires external scripts. But I will try to better exemplify with images.

2 answers

7

You can, for example, register an event by clicking on the page that will cancel the click on certain elements if your menu is open/visible:

$("body")
    .find('a:not(#meuElementoClicavel)')
    .on("click", function(e) {
        if ($('#meuElementoClicavel').is(':visible')){
            return false;
        }
    });

View Example in Jsfiddle

In the practical example you can see that with the element #meuElementoClicavel visible, the links page does not work.

In this other example, the code is present on the page, but as the element #meuElementoClicavel is not visible, the links work normally.


Applying this to the practical case of your code in question:

$('.wrapp').on('click', function(){
    if ($('#sidr').is(':visible')) {
        $.sidr('close', 'sidr');
        return false;
    }
});

Although in your practical case other things can influence the proper functioning of the "cancel" click action...

  • I had already tried to return false and it did not help. I will test your second option, but the problem is that I need to cancel any click on the site, from links to inputs and tals.

  • In order to cancel the click on all elements except the X element, you can use: $("body > *").not("#meuElementoClicavel").on("click", function(e) { ...}); See example in Jsfiddle.

  • I tested leaving only the return false and it worked!! However, I need to perform the close menu function, so I put $.sidr('close', 'sidr'); before the returnand it didn’t work anymore. If I put the return false before, also does not work.

3

If .wrapp is an overlay on the clickable objects, replace: $(this).click(false); for: event.stopPropagation();

$('.wrapp').on('click', function(event){
    if ( ($('#sidr').is(':visible')) ) {
        event.stopPropagation();
        $.sidr('close', 'sidr');
    }
});

But if the clickable objects are inside .wrapp use the: event.preventDefault();

$('.wrapp').on('click', function(event){
    if ( ($('#sidr').is(':visible')) ) {
        event.preventDefault();
        $.sidr('close', 'sidr');
    }
});
  • No gave, the links continue running.

  • @Ricardo then the .wrapp not exactly like an overlay...

  • I may be putting the wrong element. I put the class .wrapp for she is the class of divs of the contents, within each block there are links, texts, etc.

  • @Ricardo edited the answer, and keep an eye on the Event parameter in Function: function(event){

  • The event.preventDefault(); didn’t work either. :(

Browser other questions tagged

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