Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
How to change class in a list of items just to element that i clicked

I want to add to take a class of an element to click, to the first click to put my class and to the second to take out my class. I only managed to add the class to the first click and I can't get it back to normal on the second click on the item. I also tried toggle but something is missing and I don't know what. Thanks :D

const items = document.querySelectorAll(".item");

items.forEach(item => {
  item.addEventListener('click', function() {
    items.forEach(el => el.classList.remove('active'));
    this.classList.add('active');
  })
});
.list {
  margin: 100px auto;
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 700px;
}

.item {
  width: 100px;
  aspect-ratio: 1;
  background-color: blueviolet;
}

.active {
  background-color: brown;
}
<div class="list">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have to store the active state of the clicked element before removing the active class from all the elements. Then, you can set the active state of the clicked element to the opposite of what it was.

const items = document.querySelectorAll(".item");

items.forEach(item => {
  item.addEventListener('click', function() {
    // Get the active state of the clicked element
    let active = this.classList.contains("active");
    items.forEach(el => el.classList.remove('active'));
    // Toggle the active state of the clicked element
    if(active){
      this.classList.remove("active");
    } else {
      this.classList.add("active");    
    }
  })
});
.list {
  margin: 100px auto;
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 700px;
}

.item {
  width: 100px;
  aspect-ratio: 1;
  background-color: blueviolet;
}

.active {
  background-color: brown;
}
<div class="list">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't have to loop through all elements every time. You can just look up the currently active element. This should work:

const items = document.querySelectorAll(".item");

items.forEach(item => {
  item.addEventListener('click', function(){
    // Check if clicked element is active
    if (this.classList.contains("active")){
      // Element was active, deactivate and terminate
      this.classList.remove("active");
      return;
    }
    // Element clicked was not active
    // Find active element in list and deactivate, if any
    var activeElement = document.querySelector(".list .active");
    if (activeElement != null) activeElement.classList.remove("active");
    this.classList.add("active");
  })
});

See live demo.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda