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

87
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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