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

112
Views
removing row inside table with jquery

I have a table where I'm dynamically appending and removing rows. When pressing 'remove' I want to remove the whole row. This works cleanly with the already existing rows, but when using the same function for the new rows it removes everything.

How can I remove just the newly created rows, not all rows?

I have tried using find() to find the tr but it doesn't work.

$("#addRow").click(function() {
  $("#table").append(
    `<tr>`,
    `<td></td>`,
    `<td></td>`,
    `<td></td>`,
    `<td></td>`,
    `<td></td>`,
    `<td class="remove"><button>Remove</button></td>`,
    `</tr>`
  );

  $('.remove button').on('click', function() {
    removeRow($(this))
  });
})

$('.remove button').on('click', function() {
  removeRow($(this))
});

function removeRow(row) {
  row.parent().parent().remove();
}
#table {
  width: 80%;
  margin-left: auto;
  margin-right: auto;
}

#table td {
  width: 20%;
  border: solid 1px black;
}

#table .remove {
  border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td class="remove"><button>Remove</button></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td class="remove"><button>Remove</button></td>
  </tr>
</table>
<p><button id="addRow">Add a new row</button></p>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem is because you're appending the new HTML content as individual elements, so it is not being generated correctly. If you use the DOM inspector after you call append() you will see the issue.

To fix this, concatenate the HTML strings together and append() them in a single operation:

$("#table").append(
  `<tr>` + 
  `<td></td>` +
  `<td></td>` +
  `<td></td>` +
  `<td></td>` +
  `<td></td>` +
  `<td class="remove"><button>Remove</button></td>` +
  `</tr>`
);

That being said, the code you're using is not ideal and should be improved.

Firstly, use a <template /> element to store the HTML to be dynamically created within your actual HTML page. Avoid putting HTML within JS as much as possible. It's bad practice to do so due to it being a violation of the Separation of Concerns principle.

From there, use jQuery's closest() method to find the parent tr element to be removed instead of chaining multiple parent() calls.

Finally, use a single delegated event handler on the .remove button so that you don't need to re-bind it every time a new row is created.

With those corrections applied, your code should look like this:

let rowTemplate = $('#row-template').html();

$("#addRow").click(function() {
  $("#table").append(rowTemplate);
})

$(document).on('click', '.remove button', e => {
  $(e.target).closest('tr').remove();
});
#table {
  width: 80%;
  margin-left: auto;
  margin-right: auto;
}

#table td {
  width: 20%;
  border: solid 1px black;
}

#table .remove {
  border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td class="remove"><button>Remove</button></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td class="remove"><button>Remove</button></td>
  </tr>
</table>
<p><button id="addRow">Add a new row</button></p>

<template id="row-template">
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td class="remove"><button>Remove</button></td>
  </tr>
</template>

about 4 years ago · Santiago Trujillo 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!