35

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
  • 18,808
  • 10
  • 90
  • 132
Richbits
  • 7,344
  • 8
  • 33
  • 38

7 Answers7

56

this would do:

$('#selectId').html('');
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 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
30

This works too.

$("#selectId option").remove(); 
Chuck D
  • 1,629
  • 2
  • 16
  • 32
AKX
  • 152,115
  • 15
  • 115
  • 172
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
  • 14,029
  • 41
  • 106
  • 178
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.
  • 573
  • 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,298
  • 1
  • 23
  • 24
1

If without jquery:

javascript SELECT remove()

lance
  • 16,092
  • 19
  • 77
  • 136
0

Try This for Multiple Select Drop Down.

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

Ahsan Aftab
  • 181
  • 2
  • 6