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

92
Visualizações
Change parent's class with jQuery

There are dropdown groups for certain links. This is the part of them:

If the li has active-item class under level-1, then i try to replace parent li's close-item with open-item.

This is jquery code for this.

$(document).ready(function() {

  $('.active-item').parent('li').addClass('open-item').removeClass('close-item');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="has-child-item close-item">
  <a><i class="fa fa-cubes" aria-hidden="true"></i><span>İstasyon Ayarları</span></a>
  <ul class="nav child-nav level-1">
    <li class="active-item"><a href="{{url('/istasyonlar')}}">İstasyon Listesi</a></li>
    <li><a href="{{url('/istasyonlar/ekle')}}">Yeni İstasyon Tanımlama</a></li>
  </ul>
</li>

But it's not working.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

.active-item is not a parent of li it IS the li, you want:

.closest("li.close-item")
over 4 years ago · Santiago Trujillo Relatório

0

Maybe try this :

$('.active-item').parents().closest('li').addClass('open-item').removeClass('close-item');

it will change the closest li

over 4 years ago · Santiago Trujillo Relatório

0

Using closest()

closest() selects the first element that matches the selector, up from the DOM tree.

FYI parent()

parent() selects one element up the DOM tree.

Solution

$(document).ready(function() {
  $('.active-item').closest('li.has-child-item').toggleClass('open-item close-item');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="has-child-item close-item">
  <a><i class="fa fa-cubes" aria-hidden="true"></i><span>İstasyon Ayarları</span></a>
  <ul class="nav child-nav level-1">
    <li class="active-item"><a href="{{url('/istasyonlar')}}">İstasyon Listesi</a></li>
    <li><a href="{{url('/istasyonlar/ekle')}}">Yeni İstasyon Tanımlama</a></li>
  </ul>
</li>

over 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