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

How Did The Restaurant Industry change In Post Pandemic Era?

The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…

1 day ago

A Guide To CPQ and How It Fuels Marketing and Sales

Sales and marketing teams help attract, convert, and retain customers to ensure an organization’s long-term…

1 week ago

Top Questions Asked About Invoice Financing Applications

Are you an owner of a small business who’s trying to come up with ways…

2 weeks ago

Complete Medical Guide to Blood Sugar Testing and Glucose Control

Introduction When patients bring me their lab reports, the confusion is almost always the same.…

3 weeks ago

6 Tips to Strategically Remodel Your Home

Are you excited about remodeling your house after a long time? Perhaps if you're planning…

3 weeks ago

Driving Finding Balance on and off the Mat: How Calm Awareness Supports Everyday

The practice of yoga teaches us to be present, patient and mindful of our decisions.…

3 weeks ago