DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
jQuery

Remove li elements of ul except first and last in jQuery

Share

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();
Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

View Comments

Published by
Jacob Frazier
Tags: jquery

Recent Posts

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

13 hours ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

2 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

2 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

4 days ago

Essential Elements of a Franchise Marketing Strategy

No matter how much competition is there, a well-planned franchise marketing strategy can scale up…

5 days ago