25

I have a dropdown as seen below:

<select id="models" onchange="removeElements()">
            <option id = "remove" value="0">R8</option>
            <option id = "remove" value="1">Quattro</option>
            <option id = "remove" value="2">A6 hatchback</option>
</select>

How would I create the script removeElements() that would remove all of the options within the select?

SharpC
  • 6,974
  • 4
  • 45
  • 40
TotalNewbie
  • 1,004
  • 5
  • 13
  • 25
  • 4
    You understand that id section is meant to have a unique name, not as your option tags have. – gsamaras Apr 10 '14 at 12:51
  • Repeated - http://stackoverflow.com/questions/3364493/how-do-i-clear-all-options-in-a-dropdown-box – Christian Apr 10 '14 at 12:54
  • Plain JS: `document.getElementById('models').length = 0` does it for me. If you already have a reference to the select, then `select.length = 0` (see [HTML5 section 4.10.7](http://www.w3.org/html/wg/drafts/html/master/forms.html#dom-select-length)). – RobG Apr 10 '14 at 13:20

7 Answers7

80

You didn't say on which event.Just use below on your event listener.Or in your page load

$('#models').empty()

Then to repopulate

$.getJSON('@Url.Action("YourAction","YourController")',function(data){
 var dropdown=$('#models');
dropdown.empty();  
$.each(data, function (index, item) {
    dropdown.append(
        $('<option>', {
            value: item.valueField,
            text: item.DisplayField
        }, '</option>'))
      }
     )});
Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39
13

You can either use .remove() on option elements:

.remove() : Remove the set of matched elements from the DOM.

 $('#models option').remove(); or $('#models').remove('option');

or use .empty() on select:

.empty() : Remove all child nodes of the set of matched elements from the DOM.

 $('#models').empty();

however to repopulate deleted options, you need to store the option while deleting.

You can also achieve the same using show/hide:

$("#models option").hide();

and later on to show them:

$("#models option").show();
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Anyone using JavaScript (as opposed to JQuery), might like to try this solution, where 'models' is the ID of the select field containing the list :-

var DDlist = document.getElementById("models");
while(DDlist.length>0){DDlist.remove(0);}
0
function removeElements(){
  $('#models').html('');
}
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

Try this

function removeElements(){
    $('#models').html("");
}
Dhambha
  • 82
  • 1
  • 8
0

In case .empty() doesn't work for you, which is for me

function SetDropDownToEmpty() 
{           
$('#dropdown').find('option').remove().end().append('<option value="0"></option>');
$("#dropdown").trigger("liszt:updated");          
}

$(document).ready(
SetDropDownToEmpty() ;
)
Jesse
  • 65
  • 1
  • 17
0

Other approach for Vanilla JavaScript:

for(var o of document.querySelectorAll('#models > option')) {
  o.remove()
}
Piotr
  • 1,777
  • 17
  • 24