Clear dropdownlist with JQuery

how to reset multiselect checkbox dropdown in jquery
jquery reset dropdown to first option
how to clear dropdownlist using javascript
how to refresh dropdownlist using jquery
jquery clear dropdown except first option
how to clear multi select dropdown in javascript
clear select options jquery
how to clear the selected value of dropdownlist in angularjs

I wrote this little function to fill a drop down list with data from the server.

function fillDropDown(url, dropdown) {
    $.ajax({
        url: url,
        dataType: "json"
    }).done(function (data) {
        // Clear drop down list
        $(dropdown).find("option").remove(); <<<<<< Issue here
        // Fill drop down list with new data
        $(data).each(function () {
            // Create option
            var $option = $("<option />");
            // Add value and text to option
            $option.attr("value", this.value).text(this.text);
            // Add option to drop down list
            $(dropdown).append($option);
        });
    });
}

I can then call the function in this way:

fillDropDown("/someurl/getdata", $("#dropdownbox1"))

Everything is working perfectly, except for the one line where I'm clearing old data from the drop down list. What am I doing wrong?

Any tips that might help to improve this code are also highly appreciated.


Just use .empty():

// snip...
}).done(function (data) {
    // Clear drop down list
    $(dropdown).empty(); // <<<<<< No more issue here
    // Fill drop down list with new data
    $(data).each(function () {
        // snip...

There's also a more concise way to build up the options:

// snip...
$(data).each(function () {
    $("<option />", {
        val: this.value,
        text: this.text
    }).appendTo(dropdown);
});

Clear dropdownlist with JQuery, In this article I will explain with an example, how to delete / remove / clear all items (options) from DropDownList (DropDown) using JavaScript and jQuery. Delete / Remove / Clear all items (Option) from DropDownList using jQuery The following HTML Markup consists of a DropDownList control and a Button. When the Button is clicked, the jQuery click event handler is executed.


I tried both .empty() as well as .remove() for my dropdown and both were slow. Since I had almost 4,000 options there.

I used .html("") which is much faster in my condition. Which is below

  $(dropdown).html("");

Reset (Clear) DropDownList selection (selected value) using , Here Mudassar Ahmed Khan has explained with an example, how to reset (clear) DropDownList selection (selected value) using JavaScript and jQuery. The person was using the following jQuery to try and clear the items, but it wasn’t working: $( "select[id$=ddlPeople]" ).remove(); The items weren’t being removed because the jQuery Selector is wrong.


<select id="ddlvalue" name="ddlvaluename">
<option value='0' disabled selected>Select Value</option>
<option value='1' >Value 1</option>
<option value='2' >Value 2</option>
</select>

<input type="submit" id="btn_submit" value="click me"/>



<script>
$('#btn_submit').on('click',function(){
      $('#ddlvalue').val(0);
});
</script>

How to reset a dropdown list using jQuery, There are many ways to reset a dropdown list (html select control) using jQuery. Suppose we have a select control that contains 4 countries : [code]<select  Reset (Clear) DropDownList selection (selected value) using jQuery The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the jQuery click event handler is executed.


How about storing the new options in a variable, and then using .html(variable) to replace the data in the container?

Clear Items of a DropDownList using jQuery, A user on a forum recently asked a question on how you clear the items in a drop down list using jQuery. <asp:DropDownList ID="ddlPeople" runat="server"> Delete / Remove / Clear all items (Option) from DropDownList using jQuery The following HTML Markup consists of a DropDownList control and a Button. When the Button is clicked, the jQuery click event handler is executed.


How to Reset dropdown using jQuery, These are one line of jQuery code, which can reset the dropdown. Below code will find the first item in the list and make it selected. Reset (Clear) DropDownList selection (selected value) using jQuery. The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the jQuery click event handler is executed. Inside this event handler, the SelectedIndex property of the DropDownList is set to 0 (First Item).


How to Clear Dropdown list Values using jQuery & Bind New Values , how to clear dropdown values using jQuery and bind new values to dropdownlist using jQuery or clear items of dropdownlist using jQuery. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to remove all options from a dropdown using jQuery / JavaScript


Resetting all dropdown lists inside a div, I need to clear any data that may have been previously entered into code that would effectively reset all dropdown lists within a given div? Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Clear and refresh jQuery Chosen dropdown list