Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!