Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove current row from table in jQuery?

Tags:

html

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?

like image 472
Amit Avatar asked Jan 29 '10 12:01

Amit


2 Answers

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.

like image 162
cgp Avatar answered Nov 01 '22 04:11

cgp


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>
like image 9
Sarfraz Avatar answered Nov 01 '22 05:11

Sarfraz