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

262
Views
jQuery: how to remove class from nearest elements in parent div?

I have next HTML:

<table>
    <tr>
        <td>
            <span class="btn active">One</span>
            <span class="btn">Two</span>
            <span class="btn">Three</span>
        </td>
    </tr>
    <tr>
        <td>
            <span class="btn">One</span>
            <span class="btn active">Two</span>
            <span class="btn">Three</span>
        </td>
    </tr>
</table>

I try to add class active to the current clicked element:

$(document).ready(function() {
    $(".btn").click(function() {
        $(".btn").removeClass("active");
        $(this).addClass("active");
    });
});

it works, but removes active class from all other spans.

How to remove active class only from the nearest elements, for example only inside the parent td?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can make sure you are removing the class only from the children of the parent element of the clicked span.

$(document).ready(function() {
    $(".btn").click(function() {
        $(this.parentElement.childNodes).removeClass("active");
        $(this).addClass("active");
    });
});
about 4 years ago · Juan Pablo Isaza Report

0

To do what you require you can use the this keyword in the event handler to reference the element which raised the event. From there you can use jQuery's DOM traversal methods, in this case siblings(), to find the related elements and change their classes.

jQuery($ => {
  $(".btn").click(function() {
    $(this).addClass('active').siblings('span').removeClass("active");
  });
});
.active { color: #C00; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <span class="btn active">One</span>
      <span class="btn">Two</span>
      <span class="btn">Three</span>
    </td>
  </tr>
  <tr>
    <td>
      <span class="btn">One</span>
      <span class="btn active">Two</span>
      <span class="btn">Three</span>
    </td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Report

0

I find solution:

$(document).ready(function() {
  $(".btn").click(function() {
    $(this).parent().find('.btn').removeClass("active");
    $(this).addClass("active");
  });
});
.active {
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <span class="btn active">One</span>
      <span class="btn">Two</span>
      <span class="btn">Three</span>
    </td>
  </tr>
  <tr>
    <td>
      <span class="btn">One</span>
      <span class="btn active">Two</span>
      <span class="btn">Three</span>
    </td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza 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!