leora
leora

Reputation: 196861

what is the easiest way to remove all entries from a select dropdown using jquery?

i have a dropdown and i want to clear all items from it using jquery. i see a lot of google links about removing selected item but i want to clear ALL items from a dropdown.

what is the best way of removing all items from a select dropdown list?

Upvotes: 14

Views: 16780

Answers (3)

xkeshav
xkeshav

Reputation: 54074

BEST way: use .empty()

$('select').empty();

DEMO

Note: Use .remove() when you want to remove the element itself, as well as everything inside it

Upvotes: 41

Boldewyn
Boldewyn

Reputation: 82814

$('option', the_select_element).remove();

If you want to keep the selected:

$('option:not(:selected)', the_select_element).remove();

It's really simple in plain JS, too (thanks, @Austin France!):

// get the element
var sel = document.getElementById("the_select_ID");
// as long as it has options
while (sel.options.length) {
  // remove the first and repeat
  sel.remove(0);
}

Upvotes: 3

Richard Dalton
Richard Dalton

Reputation: 35803

$('#idOfDropdown option').remove();

JSFiddle Example

Upvotes: 7

Related Questions