JQuery removal of an element and its brother

I have a table with an even number of rows.

Odd lines are visible and they have a delete button, even the lines are hidden.

Delete: delete a pair, an odd line and a hidden even line.

The next line deletes the odd line. How to delete an odd line and the next brother?

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

Many thanks

+4
source share
3 answers

You can use the addBack () jQuery method:

$tr.next('tr').addBack().remove();
+9
source

Delete first sibling, since deleting the source rowwill first result in an error when trying to access the siblingdeleted row.

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

Use it, it will work,

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

Source: https://habr.com/ru/post/1526518/


All Articles