Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

.closest("li.close-item")
about 4 years ago · Santiago Trujillo Report

0

Maybe try this :

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

it will change the closest li

about 4 years ago · Santiago Trujillo Report

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>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!