Amit
Amit

Reputation: 26266

How to remove current row from table in jQuery?

I have a table in html as follows

<table>
<tbody>
<tr>
<td>test content</td>
<td><input type="button" onClick="remove()"></td>
</tr>
....
...

</tbody>
</table>

now if the same pattern continues, i want to remove a row if a remove button is clicked on that row. how do i achieve the same with jQuery?

Upvotes: 15

Views: 36369

Answers (2)

Sarfraz
Sarfraz

Reputation: 382696

Try this:

<input type="button" onClick="$(this).parent().parent().remove();">

Or you can make it more generic like this:

<script>
  $(document).ready(function()
  {
    $(".btn").click(function(){
      $(this).parent().parent().remove();
    });
  });
</script>

<tr>
  <td><input type="button" class="btn"></td>
</tr>

Upvotes: 9

cgp
cgp

Reputation: 41381

Nicer:

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

More on closest()

<input type="button" onClick="$(this).closest('tr').remove();">

This has the benefit of working no matter what your HTML looks like in the cell.

Upvotes: 62

Related Questions