Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add multiple classes with 1 button

I’m trying to add multiple classes to different items. I use this code:

    (function() {
    
      var button = document.querySelector('.menu-toggle');
      var box = document.querySelector('.menu-toggle2');
    
      button.addEventListener('click', function() {
    
        box.classList.toggle('active');
    
      });
    
    })();

   <button class="menu-toggle menu-toggle2"></button>
   <div class="menu-mob"></div>

after click:

<button class="menu-toggle menu-toggle2 active"></button>
<div class="menu-mob"></div>

And after click, I want to add activemenu class to mob-menu class too.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can achieve this by selecting the menu-mob element and adding the activeMenu class to it inside the event listener. Here is the modified code:

(function() {
  var button = document.querySelector('.menu-toggle');
  var box = document.querySelector('.menu-toggle2');
  var menuMob = document.querySelector('.menu-mob');

  button.addEventListener('click', function() {
    box.classList.toggle('active');
    menuMob.classList.toggle('activeMenu');
  });
})();

This will add the activeMenu class to the menu-mob element when the button is clicked and the active class is added to the menu-toggle2 element.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading