How do I detect a click out of an element?

Asked

Viewed 87 times

0

I have some HTML menus, which I show when a user clicks on these menus. I would like to hide these elements when the user clicks out of of the menu area.

How could I do that?

  • event onfocusout active when you remove the focus of some element.

1 answer

1

add a class . menus and then jquery:

$('body').on('click', function () { // Pega o clique
    if (!$(this).hasClass('menus')) { //Se o objeto clicado não possuir a classe, então o clique foi fora dos menus
        $('.menus').hide(); // esconde os menus
    }
});

Browser other questions tagged

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