OM The Eternity
OM The Eternity

Reputation: 16244

How to remove few options from select box using jquery?

I want to remove few options, say, last three options, from a select box out of 5 options, using jquery

Below is the select box with 5 options tag

<select id="select">
  <option>first</option>
  <option>second</option>
  <option>third</option>
  <option>fourth</option>
  <option>fifth</option>
</select>

UPDATE:

I tried this code

$("#select option:not(option:first, option:last)";

But that gave me uncaught exception in console

Upvotes: 1

Views: 599

Answers (3)

you can use the following code to remove the latest items http://jsfiddle.net/QxmvC/

     $('#select').children().slice(-3).remove();

Upvotes: 0

Mike
Mike

Reputation: 2136

What have you tried? I think the nth-child selector might be what you're looking for.

Upvotes: 0

VisioN
VisioN

Reputation: 145458

slice() method is the way to go:

$("#select > option").slice(-3).remove();

Upvotes: 7

Related Questions