Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select list removes all options

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?

like image 683
Richbits Avatar asked Jul 09 '09 15:07

Richbits


People also ask

How do you remove all options from select in jQuery except first?

Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').

How do I select a specific DropDownList using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How do you reset a drop down?

The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the Reset JavaScript function is executed. Inside this function, the SelectedIndex property of the DropDownList is set to 0 (First Item).


2 Answers

this would do:

$('#selectId').html(''); 
like image 127
David Hedlund Avatar answered Sep 21 '22 17:09

David Hedlund


This works too.

$("#selectId option").remove();  
like image 20
AKX Avatar answered Sep 25 '22 17:09

AKX