Homer_J
Homer_J

Reputation: 3323

jQuery remove() everything except an item

I currently have the following code:

$( '.spandiv:eq(1)' ).remove();

This very nicely removes the second (as it uses 0-based indexing) item in the span list, which is great. However, I have a question.

Is it possible to remove everything except the second item?

I know I could code up using the lt() and gt()' but wondered if there was anot equal` option?

Upvotes: 0

Views: 69

Answers (2)

Dhaval Bharadva
Dhaval Bharadva

Reputation: 3083

You can use .not()

$('.spandiv').not(':eq(1)').remove();  

JSFIDDLE DEMO

Upvotes: 1

adeneo
adeneo

Reputation: 318322

You can use :not or .not()

$( '.spandiv:not(:eq(1))' ).remove();

FIDDLE

Upvotes: 2

Related Questions