Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery removing an element and its sibling

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

like image 471
Alan Alcock Avatar asked Feb 12 '14 11:02

Alan Alcock


2 Answers

You could use addBack() jQuery method:

$tr.next('tr').addBack().remove();
like image 118
A. Wolff Avatar answered Nov 16 '22 18:11

A. Wolff


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();
});
like image 25
Adil Avatar answered Nov 16 '22 17:11

Adil