5

I have a table that has an even number of rows.

Odd rows are visible and have a delete button in them, even rows are hidden.

Delete should remove a pair, the odd row and the hidden even row.

The following only removes the odd row. How can I remove the odd row and the next sibling?

$('deleteButton').click(function() {
    var $tr = $(this).closest('tr');
    $tr.remove().next('tr').remove();
});

Many thanks

Alan Alcock
  • 787
  • 1
  • 11
  • 26

3 Answers3

10

You could use addBack() jQuery method:

$tr.next('tr').addBack().remove();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1

Remove sibling first as removing the source row first will cause an error when trying to access the sibling of removed row.

$('deleteButton').click(function() {
    var $tr = $(this).closest('tr');
    $tr.next('tr').remove();
    $tr.remove();
});
Adil
  • 146,340
  • 25
  • 209
  • 204
0

Use this,It will work,

$('deleteButton').click(function() {
    var $tr = $(this).closest('tr');
    $tr.next('tr').remove();
    $tr.remove();
});
Shivam
  • 702
  • 2
  • 10
  • 25