Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap li into another li

I have below ul list. In which I want to wrap last three li into another ul li tag.

My html

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

I want

<ul>
<li>1</li>
<li>
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</li>
</ul>

my code

$('ul li:not(:first)').wrap('<li></li>');

But it's wraping individual li into another li. pls help

like image 539
Rakesh Kumar Avatar asked Mar 19 '23 21:03

Rakesh Kumar


1 Answers

All you need here in this context is .wrapAll()

Try,

$('ul li:not(:first)').wrapAll('<li><ul></ul></li>');

DEMO

like image 85
Rajaprabhu Aravindasamy Avatar answered Mar 28 '23 12:03

Rajaprabhu Aravindasamy