How to remove all li elements inside an ul except the first and the last elements using jQuery?

To remove from all ul-s on the page the li-s that are neither the first nor the last:

$('ul li:not(:first-child):not(:last-child)').remove();

Or using a specific id:

$('#yourul li:not(:first-child):not(:last-child)').remove();

If you have a jQuery object with your ul in it:

$(yourul).find('li:not(:first-child):not(:last-child)').remove();

Have you tried selecting all the list elements, and then removing the first and last ones from the list?

$('ul li').not('li:first, li:last').remove()