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

92
Vistas
Select A Button By Class jQuery

I am trying to have a button that, when clicked, will have some jQuery code linked to it. See the general layout below. However, there will be a variable number of rows on the table. All the buttons should have the same action but applied to its parent element. How can I achieve this?
Thanks

td {
  border: 1px solid black;
  padding: 10px;
}
<table>
  <tr>
    <td>Info</td>
    <td><button class="remove">Remove row</button></td>
  </tr>
  <tr>
    <td>Info</td>
    <td><button class="remove">Remove row</button></td>
  </tr>
  <tr>
    <td>Info</td>
    <td><button class="remove">Remove row</button></td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can do that in several ways, question is tagged with jQuery so the easiest way using jQuery is :

$("button").on("click", function(ev) {
    $(this).parents("tr").remove()
});

This will remove tr element when clicked on it's button, you can do what you want inside that anonymous function

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this

let btns = $("button.remove"); // you will select only buttons with remove class

in your case you want something like this :

btns.each(function(){
let btn  = $(this);
   btn.on('click',()=>{
    // your stuff
   })
})

i hope it was useful !

about 4 years ago · Juan Pablo Isaza Denunciar

0

The example below does what you need, it will check any click within a table element to see if it matches the required pattern (in this case a button with class .remove), before then actioning the code.

I've added a dynamically added row so you can test it.

If you aren't having dynamically added rows (i.e. they are all there before this code is run) then you can simply use:

$("table button.remove").click( function() {

    $(this).closest("tr").remove();

});

// Add click event linked to the table
// Will trigger whenever a button with class .remove is clicked within the table
$( "table" ).on( "click", "button.remove", function() {

  // Move up DOM tree to nearest 'tr' and remove it
  $(this).closest("tr").remove();

});


// Add click event to add row button
$("#add-row").click( function() {

  // Add dynamic row for testing
  $("table").append('<tr><td>Info</td><td><button class="remove">Remove row</button></td></tr>');

});
td {
  border: 1px solid black;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td>Info</td>
    <td><button class="remove">Remove row</button></td>
  </tr>
</table>

<button id="add-row">Add Row</button>

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