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

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

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 Denunciar

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