JavaScript Toggle Class Multiple Elements

JavaScript Toggle Class Multiple Elements
Code Snippet:Javascript Active Toggle
Author: M.McGraw
Published: January 12, 2024
Last Updated: January 22, 2024
Downloads: 574
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code enables the toggling of an “active” class among multiple elements. The code defines a function to toggle the class and attaches it to specific HTML elements. When clicked, these elements dynamically switch their active state, enhancing user interaction. This code is helpful for managing and visually highlighting selected elements within a group.

This code is beneficial for interactive web interfaces, allowing users to toggle the “active” class on multiple elements.

How to Create Javascript Toggle Class Multiple Elements

2. Begin by creating the HTML structure with elements you want to toggle. Each element should have a unique ID and share a common parent, as shown in the following code.

<div id="hold">
  <div class="box" id="one"></div>
  <div class="box" id="two"></div>
  <div class="box" id="three"></div>
</div>

2. Style your elements and parent container using the included CSS code. Customize the design to fit your project’s needs.

html {
  background: rgb(34, 34, 34);
  user-select: none;
  -moz-user-select: none;
}
h2 {
  font-family: 'Luckiest Guy', cursive;
  color: rgb(120, 196, 201);
  text-align: center;
  font-size: 64px;
  margin-top: 50px;
}
.box {
  width: 100px;
  height: 100px;
  cursor: pointer;
}

.box:hover {
  opacity: 0.6;
}

.active {
  border-radius: 100%;
}

#hold {
  width: 325px;
  height: 100px;
  margin: 50px auto;
  color: rgb(237, 94, 59);
  text-align: center;
  font-family: impact;
  font-size: 24px;
}

#hold > div {
  display: inline-block;
  margin-right: 3px;
}

#one {
  background: rgb(237, 94, 59);
  width: 100px;
  height: 100px;
}

#two {
  background: rgb(87, 154, 166);
  width: 100px;
  height: 100px;
}

#three {
  background: rgb(168, 168, 168);
  width: 100px;
  height: 100px;
}

3. Finally, add the provided JavaScript code to your project. This code utilizes vanilla JavaScript to toggle the “active” class on click, creating a dynamic and interactive experience.

//vanilla js -- toggle active class 
// el = object containing the elements to toggle active class and the parent element 
var el = {
  one: document.getElementById('one'),
  two: document.getElementById('two'),
  three: document.getElementById('three'),
  hold: document.getElementById('hold')
};
// func = object containing the logic 
var func = {
  toggleActive: function(ele) {
    ele = event.target;
    var hold = el.hold.children,
        huh = el.hold.children.length,
        hasActive = ele.classList.contains('active');
    for (i = 0; i < huh; i++) {
      if (hold[i].classList.contains('active')) {
        hold[i].classList.remove('active');
      }
    }
    if (!hasActive) {
      ele.classList.add('active');
    }
  }
};

//add listeners when the window loads 
window.onload = function() {
  el.one.addEventListener("click", func.toggleActive);
  el.two.addEventListener("click", func.toggleActive);
  el.three.addEventListener("click", func.toggleActive);
};

Modify the JavaScript code according to your project’s needs. You can easily integrate this functionality into various projects such as image galleries, menu systems, or any scenario where selecting and highlighting elements is required.

That’s all! hopefully, you have successfully created functionality in JavaScript to toggle a class for multiple elements on a webpage. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X