O
O
Oleg Morev2018-02-15 17:37:51
JavaScript
Oleg Morev, 2018-02-15 17:37:51

How to hide an element on click?

Good afternoon, tell me a solution in native JS.
The essence of the question in the menu is a part that leaves and the .show class is assigned to it. It is
necessary that when clicking outside this menu, the .show class is deleted.
May come in handy :)

toggleMenu() {

    var menuButton = document.getElementById('menuButton');
    var menuOverlay = document.getElementById('menuOverlay');  

    menuButton.classList.toggle('show');
    menuOverlay.classList.toggle('show');

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Morev, 2018-02-20
@DpOLEGapx

Solved the problem :) suddenly someone will come in handy

document.addEventListener('DOMContentLoaded', function() {

var menuOverlay = document.getElementById('menuOverlay');

document.addEventListener("click", function() { 
menuOverlay.classList.remove('show');
});

menuOverlay.addEventListener("click", function(e) { 
e.stopPropagation();
});

});

D
Don2Quixote, 2018-02-19
@Don2Quixote

I don’t remember how to do it in pure js, but I advise you to get acquainted with jQuery )
Everything is simple there:
To hide an element: To show:
$("#id или .class элемента").show();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question