0

I have simplified the situation as shown below:

 <table>
  ...
  <tr><td>....</td><td><button...>delete</button></td></tr>
  <tr><td>somestuff</td><td>some other stuff</td></tr>

 </table>

On click of the delete button, I delete the row by using:

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

My question is, how do I delete the next row too using some CSS selector for the next row. I have a work-around by using a running id number for each row, picking the id # of the one deleted, incrementing it and then using it to delete the next row, but I think there must be some CSS selector to select the next row.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sunny
  • 9,245
  • 10
  • 49
  • 79

3 Answers3

2

Removing both at once:

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

.addBack() replaces .andSelf() for jQuery 1.8 and later.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
1
    $(this).closest('tr').next('tr').remove();
  $(this).closest('tr').remove();
Irfan TahirKheli
  • 3,652
  • 1
  • 22
  • 36
  • TahiKheli. Yes. It should work too. Thanks. Upvoting. – Sunny Jan 28 '14 at 20:37
  • TahiKheli. Yes. It should work too. Thanks. Upvoting. Coming to think of it, yours is the best solution technically. Just that in my situation, the answer accepted would be easier to document because I need to specify which rows I am deleting and the intermediate variable names serve that purpose. THANKS anyway. – Sunny Jan 28 '14 at 20:44
  • Primary aim to resolve your issue. Glad it is resolved. Cheers buddy happy coding. :) – Irfan TahirKheli Jan 28 '14 at 20:47
0

Your best bet is to separate finding the rows, and removing them - otherwise you'll get unpredictable results.

var rowA = $(this).closest('tr');
var rowB = rowA.next();

rowA.remove();
rowB.remove();
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    Your answer requires the least amount of understanding of what-if possibilities of what happens once you delete the row. Is a reference to its next still defined? etc... Accepting. Thanks! – Sunny Jan 28 '14 at 20:32
  • and helps document which rows are deleted by substituting rowA and rowB by more descriptive names. – Sunny Jan 28 '14 at 20:45