I
I
Ivan Ivanovich2020-04-17 14:09:45
JavaScript
Ivan Ivanovich, 2020-04-17 14:09:45

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);


Please tell me what is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tigran Abrahamyan, 2020-04-17
@IwanQ

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 question

Ask a Question

731 491 924 answers to any question