Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
I want to remove class from first element while adding in other element using JavaScript

I have four div with image and I want to add a class .open when clicked on the panel and remove this class from last div when clicked on another div.

 <div class="panel panel1">
    image..
  </div>
  <div class="panel panel2">
    image..
  </div>
  <div class="panel panel3">
    image..
  </div>
  <div class="panel panel4">
    image..
  </div>
  <div class="panel panel5">
    image..
  </div>

Qhen we add open class list this image container grows 5 time so, I want to remove this class when click on other image container:

.panel.open {
    font-size: 40px;
    flex: 5;
  }



  const panel = document.querySelectorAll(".panel");
  function toggleOpen() {
    this.classList.toggle("open");
  }

  function toggelActive() {
    this.classList.add("open-active");
  }

  panel.forEach((panel) => panel.addEventListener("click", toggleOpen));
  panel.forEach((panel) => panel.addEventListener("transitionend", toggelActive));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

when you use querySelectorAll the result you will get will be a NodeList, you can access the elements via index and change them.

const divs = document.querySelectorAll("div");

console.log(divs[divs.length - 1]);

divs[divs.length - 1].classList.add("active");
.active {
  font-size: 30px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>

  <script src="./script.js"></script>
</body>
</html>

If you give a more detailed description of the logic you want to implement, maybe I can help you better.

Remembering that to access the first element the index is 0

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use a for loop to loop over all panels remove the 'open' class and then add it to the clicked panel:

const panel = document.querySelectorAll(".panel");

function toggleOpen() {
  // For loop
  for (let i = 0; i < panel.length; i++) {
    panel[i].classList.remove("open");
  }
  this.classList.add("open");
}

function toggelActive() {
  this.classList.add("open-active");
}

panel.forEach((panel) => panel.addEventListener("click", toggleOpen));
panel.forEach((panel) => panel.addEventListener("transitionend", toggelActive));
<div class="panel panel1">
    image..
  </div>
  <div class="panel panel2">
    image..
  </div>
  <div class="panel panel3">
    image..
  </div>
  <div class="panel panel4">
    image..
  </div>
  <div class="panel panel5">
    image..
  </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you could give us more details, but if you just want to close an image when you open another one, you can just remove all "open" classes from your panels, like:

const panel = document.querySelectorAll(".panel");
function toggleOpen(element) {
  element.classList.toggle("open");
}

function toggelActive(element) {
  element.classList.add("open-active");
}

/**
 * Remove all open classes from your panel
 */
function removeAll() {
  panel.forEach((panel) => {
    panel.classList.remove("open", "open-active")
  })
}

panel.forEach((panel) => panel.addEventListener("click", event => {
  /* First remove all classes */
  removeAll();
  toggleOpen(event.target)
}));
panel.forEach((panel) => panel.addEventListener("transitionend", event => {
  /* First remove all classes */
  removeAll();
  toggelActive(event.target)
}));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda