Trying to remove all li elements inside a ul except first and last elements in jQuery? Here is the code to do this.
Suppose, you have a ul li list and you need to remove all the li elements inside it except first and last li.
So, to remove from all ul-s on the page the li-s that are neither the first nor the last:
Sample HTML:
<ul> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ul>
$('ul li:not(:first):not(:last)').remove();
Or you can use with a specific id:
$('#YOURID li:not(:first):not(:last)').remove();
Let’s face it. Tech buzzwords get thrown around a lot—especially when it comes to how…
In today’s digital world, the boundaries between technology, finance, and innovation are rapidly disappearing. Businesses…
Backyard gatherings like BBQs, family reunions, and garden parties are an exciting way to enjoy…
Marketers are always on the lookout for more effective ways to reach their target audiences.…
Does your phone control your mind more than you control your phone? Modern life exploits…
Did you know that the prostate continues growing throughout a man's entire life, making BPH…
View Comments
Useful. Thanks.