views:

28534

answers:

12

What is the best method for removing a table row with jQuery?

+1  A: 

Is the following acceptable:

$('#myTableRow').remove();
Darryl Hein
i see what you did there
SoloBold
The said to do that on the podcast...just following instructions :)
Darryl Hein
I don't think Jeff meant to post useless non-questions.
John Millikin
Darryl == rep whore
Jason Bunting
JasonBunting == one to talk
Darryl Hein
+33  A: 

You're right:

$('#myTableRow').remove();

This works fine if your row has an id, such as:

<tr id="myTableRow"><td>blah</td></tr>

If you don't have an id, you can use any of jQuery's plethora of selectors.

jobscry
Yeah, I was assuming the row has an ID and you have the ID :)
Darryl Hein
+1  A: 
function removeRow(row) {
    $(row).remove();
}

<tr onmousedown="removeRow(this)"><td>Foo</td></tr>

Maybe something like this could work as well? I haven't tried doing something with "this", so I don't know if it works or not.

Eikern
Well, I would say that would be a little weird have the row disappear when you click on it. At the moment I have a link in the row to delete the row.
Darryl Hein
+10  A: 

@Eikern

If you're gonna use jQuery, use jQuery man!

$('#myTable tr').click({
    $(this).remove();
    return false;
};
nickf
nice, very non-instrusive
jobscry
A: 

$('tr').click(function() { $(this).remove(); });

i think you will try the above code, as it work, but i don't know why it work for sometime and then the whole table is removed. i am also trying to remove the row by click the row. but could not find exact answer.

+4  A: 

Assuming you have a button/link inside of a data cell in your table, something like this would do the trick...

$(".delete").live('click', function(event) {
 $(this).parent().parent().remove();
});

This will remove the parent of the parent of the button/link that is clicked. You need to use parent() because it is a jQuery object, not a normal DOM object, and you need to use parent() twice, because the button lives inside a data cell, which lives inside a row....which is what you want to remove. $(this) is the button clicked, so simply having something like this will remove only the button:

$(this).remove();

While this will remove the data cell:

    $(this).parent().remove();

If you want to simply click anywhere on the row to remove it something like this would work. You could easily modify this to prompt the user or work only on a double-click:

$(".delete").live('click', function(event) {
 $(this).parent().remove();
});

Hope that helps...I struggled on this a bit myself.

sluther
What if the link is not directly inside the td, but has, say, a span around it? I think it'd be better to do $(this).parents('tr').remove(); to let it walk up the DOM tree by itself, find the tr, and remove it.
Paolo Bergantino
That would work too. It all depends on where in the DOM your button/link is, which is why I gave so many examples and such a long explanation.
sluther
You can also use $(this).parents('tr')
cgreeno
A: 

All you have to do is to remove the table row (<tr>) tag from your table. For example here is the code to remove the last row from the table:

$('#myTable tr:last').remove();

*Code above was taken from this jQuery Howto post.

Uzbekjon
+2  A: 

You can use:

$($(this).closest("tr"))

for finding the parent table row of an element.

It is more elegant than parent().parent() which is what I started out doing and soon learnt the error of my ways.

Ian Lewis
A: 

i feel stupid, i'm trying something like this:

function doDelete(idElement) {
  rowId="#" + idElement;
  $(rowId).remove();
}

and

<tr id="TES.001">
    <td><input type="button" value="remove" onclick="doDelete('TES.001');"/></td>
</tr>

but it's not working. no error or anything, the row simply stays there...

any idea of what am i doing wrong?

Marcello
+4  A: 

Easy.. Try this

$("table td img.delete").click(function () {
    $(this).parent().parent().parent().fadeTo(400, 0, function () { 
        $(this).remove();
    });
    return false;
});
Thurein
A: 

Hi, That worked at all. Thank you so much.

Prashant
A: 

try this for size

$(this).parents('tr').first().remove();

full listing:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript">
    $(document).ready(function() {
        $('.deleteRowButton').click(DeleteRow);
      });

    function DeleteRow()
    {
      $(this).parents('tr').first().remove();
    }
  </script>
</head>
<body>
  <table>
    <tr><td>foo</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
    <tr><td>bar bar</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
    <tr><td>bazmati</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
  </table>
</body>
</html>
Tim Abell