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

152
Visualizações
jQuery toggle & remove class not correct

I have simple jQuery toggle class and remove class.

Assume I have 5 menu. Now when I click one menu then the arrow will set to down position.

Then now try to click another menu, the arrow down from previous menu is not back from the original position.

What I need is when I click one menu then click another menu then another arrow should back to the original position.

for(var i=0; i<5; i++) {
  var el = "<div class='click' style='padding: 10px; cursor: pointer;'><i class='fa-regular fa-angle-right'></i></div>";
  
  $(".menu").append(el);
}

$(".click").click(function(e) {
  $(".menu").removeClass("expanded");
    $(this).find("i").toggleClass("expanded");
});
.expanded {
  transform: rotate(90deg);
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.1.2/css/all.css">

<div class="menu"></div>

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

One solution is removeClass from all i elements except the clicked one by not method.

$(".click i").not(iEle).removeClass("expanded");

Before toggleClass for clicked one.

for(var i=0; i<5; i++) {
  var el = "<div class='click' style='padding: 10px; cursor: pointer;'><i class='fa-regular fa-angle-right'></i></div>";
  
  $(".menu").append(el);
}

$(".click").click(function(e) {
  const iEle = $(this).find("i");
  $(".click i").not(iEle).removeClass("expanded");
  iEle.toggleClass("expanded");
});
.expanded {
  transform: rotate(90deg);
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.1.2/css/all.css">

<div class="menu"></div>

about 4 years ago · Santiago Trujillo 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