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

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

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

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

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