Answer the question
In order to leave comments, you need to log in
Class not being removed?
Hello.
I can't figure out why the closeMenu function doesn't work. That is why the classes are not removed.
Most likely I'm just dumb and can't understand something obvious, but nevertheless, I didn't expect this at all.
https://jsfiddle.net/h4wc1qfa/
<button class="btn">Click me</button>
<div class="menu">
123
</div>
.menu{
display: none;
}
.responsive{
display: block;
}
const resBtn = document.querySelector('.btn'),
resMenu = document.querySelector('.menu');
const openMenu = e=>{
if(e.target){
e.preventDefault();
}
if(resBtn.classList.contains('exprended') === true){
closeMenu();
}
resBtn.classList.add('exprended');
resMenu.classList.add('responsive');
};
const closeMenu = ()=>{
resBtn.classList.remove('exprended');
resMenu.classList.remove('responsive');
};
resBtn.addEventListener('click', openMenu);
Answer the question
In order to leave comments, you need to log in
You need to stop the execution of the function with return;
so as not to add classes again.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question