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

207
Visualizações
Hide first closest parent element using javascript (or jquery)

I want to hide a html class when button clicked.

The html structure is like this:

<thead>
  <tr class="employee-list">
     <th>
         <button class="show-emp">Show Employee</button>
     </th>
  </tr>
</thead>
<tbody class="emp">
   <tr class="employee-list">
      <td style="width: 300px" class="text-center">Employee 1</td>
   </tr>
   <tr class="employee-list-last">
      <td style="width: 300px" class="text-center">Employee 2</td>
   </tr>
</tbody>
<tbody class="emp">
   <tr class="employee-list">
      <td style="width: 300px" class="text-center">Employee 1</td>
   </tr>
   <tr class="employee-list-last">
      <td style="width: 300px" class="text-center">Employee 2</td>
   </tr>
</tbody>

Also I can have more than one .emp class.

I've tried something like code bellow, but still not working (it hide all element with .emp class)

$('.show-emp').click(function() {
   $(this).closest(".emp").hide();
});

The ilustration how it works is like this: enter image description here

When show button clicked, it will only hide/show the employee list bellow it.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First of all correct your markup and jquery properly.

  1. check your <button tag
  2. Jquery code here ('.show-emp')

You can achieve it many way. one of the below way.

$('.show-emp').click(function() {
   $(this).parents().siblings('.emp').first().hide();
   
   // to hide only first sibling then use //first()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
  <tr class="employee-list">
     <th>
         <button class="show-emp"> Btn </button>
     </th>
  </tr>
</thead>
<tbody class="emp">
   <tr class="employee-list">
      <th style="width: 300px" class="text-center">Employee 1</th>
   </tr>
   <tr class="employee-list-last">
      <th style="width: 300px" class="text-center">Employee 2</th>
   </tr>
</tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

.closest() finds the closest parent element that matches the selector, but .emp isn't a parent of .show-emp.

You want to go up to the thead, then find the first sibling after it that matches the selector.

.nextAll() will find all the following siblings that match. Use .first() to get the first one.

$('.show-emp').click(function() {
  $(this).closest("thead").nextAll(".emp").first().toggle();
  $(this).text(function(i, text) {
    return text == 'Show Employee' ? 'Hide Employee' : 'Show Employee';
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr class="employee-list">
      <th>
        <button class="show-emp">Hide Employee</button>
      </th>
    </tr>
  </thead>
  <tbody class="emp">
    <tr class="employee-list">
      <td style="width: 300px" class="text-center">Employee 1</td>
    </tr>
    <tr class="employee-list-last">
      <td style="width: 300px" class="text-center">Employee 2</td>
    </tr>
  </tbody>
  <tbody class="emp">
    <tr class="employee-list">
      <td style="width: 300px" class="text-center">Employee 3</td>
    </tr>
    <tr class="employee-list-last">
      <td style="width: 300px" class="text-center">Employee 4</td>
    </tr>
  </tbody>
</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