Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery nextUntil, including, not excluding the matched element?

Is there a way to easily do nextUntil, so that the element matched by the selector is included? I have this, which is only fine if there is a previous sibling:

$("#content").find("h3:first").prev().nextUntil("ul:last").wrapAll("<div id='collapse'></div>");
like image 291
jwinn Avatar asked Mar 02 '12 19:03

jwinn


Video Answer


1 Answers

Remove .prev(), replace .nextUntil with .nextAll and use .addBack() at the end of your selector as shown below:

$("#content").find("h3:first").nextAll("ul:last").addBack().wrapAll("<div id='collapse'></div>");

Pre 1.8 should use andSelf instead of addBack

like image 134
Vadim Avatar answered Sep 30 '22 17:09

Vadim