34

I have a <select> list, which has been populated with several options, but want to remove those options to start again.

I am using jQuery and have tried the following:

$("#selectId").length = 0;

But this seems to have no effect.

Part of my problem is that I am using Firebug to debug the JavaScript, but the debugger does not break at the breakpoint, so I cannot see what is going on. Should it break when the JavaScript is within the <head> of my HTML file?

Sebastian Zartner
  • 17,621
  • 9
  • 80
  • 125
Richbits
  • 6,844
  • 8
  • 31
  • 35

7 Answers7

53

this would do:

$('#selectId').html('');
David Hedlund
  • 125,403
  • 30
  • 199
  • 217
  • doesnt this removes the – therealprashant Aug 07 '17 at 09:58
  • 1
    @therealprashant: `.html` sets the *inner* html of an element, so setting it to an empty string will remove any child nodes, but spare the dropdown list itself. – David Hedlund Aug 11 '17 at 17:36
29

This works too.

$("#selectId option").remove(); 
Chuck D
  • 1,572
  • 2
  • 15
  • 30
AKX
  • 123,782
  • 12
  • 99
  • 138
11

$("#selectId").empty(); works for me.

The $("#selectId option").remove(); removed the select from the DOM.

Currently using jQuery JavaScript Library v1.7.1

Dr. Rajesh Rolen
  • 13,643
  • 39
  • 101
  • 175
John Forbes
  • 145
  • 3
  • 8
7

The fastest way to accomplish this:

$("#selectId").empty();

Here are some tests: http://jsperf.com/find-remove-vs-empty

I take no credit for these tests. See this stack overflow question: How do you remove all the options of a select box and then add one option and select it with jQuery?

Community
  • 1
  • 1
DR.
  • 555
  • 6
  • 20
2
$("#selectId").options.length = 0;

That won't actually work as written because it's using a jQuery selector. It'd work if you used a straight DOM getElementById on the select list, though. Either AKX's or d.'s responses will set you right.

RwwL
  • 3,243
  • 1
  • 22
  • 24
1

If without jquery:

javascript SELECT remove()

lance
  • 15,790
  • 19
  • 74
  • 133
0

Try This for Multiple Select Drop Down.

$('#FeatureId').multiselect("deselectAll", false).multiselect("refresh");

Ahsan Aftab
  • 151
  • 2
  • 6