jQuery removing an element and its sibling

Alan Alcock picture Alan Alcock · Feb 12, 2014 · Viewed 8.4k times · Source

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

Answer

A. Wolff picture A. Wolff · Feb 12, 2014

You could use addBack() jQuery method:

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