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

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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