Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove next all including ($this) using Jquery

Tags:

jquery

Here's a quickie:

Is there a better jquery alternative to remove next all, including ($this)?

So far, this works for me: $(this).nextAll().remove();$(this).remove();

like image 534
Jroen Avatar asked Dec 17 '22 07:12

Jroen


2 Answers

If you want to chain your calls then there are a few options however it doesn't change much in the way of performance:

$(this).nextAll().remove().end().remove();

$(this).nextAll().add(this).remove();

$(this).nextAll().andSelf().remove();

.end():

End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.

http://api.jquery.com/end

.add():

Add elements to the set of matched elements.

http://api.jquery.com/add

.andSelf():

Add the previous set of elements on the stack to the current set.

http://api.jquery.com/andself

like image 99
Jasper Avatar answered Jan 06 '23 08:01

Jasper


$(this).nextAll().andSelf().remove();
like image 26
karnyj Avatar answered Jan 06 '23 08:01

karnyj