To remove all options from a select list using jQuery, the simplest way is to use the empty() method.
$("#select-list").empty();
Let’s say I have the following HTML:
<div id="div">
<select id="select-list">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
If we want to remove all options from the select list, we can use the jQuery empty() method to do this with the following Javascript code.
$("#select-list").empty()
The resulting HTML would be as follows:
<div id="div">
<select id="select-list">
</select>
</div>
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#select-list").empty()
Removing All Options from a Select List Using jQuery With a Click
Many times when creating a web page and the user experience, we want to remove all options from a select list based on an interaction with another element on the web page.
We can remove all options from a select using jQuery very easily by combining the empty() method with a click event.
Let’s say we have the following HTML code and we want to give the user the ability to remove all options.
<div id="div">
<p id="click-me">Click Me to Remove All Options from the Select List Below</p>
<select id="select-list">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
We can utilize both the jQuery click() method
and jQuery empty() method to remove the options from our select HTML element.Below is the Javascript code which will allow the user to be able to remove the options from our select element:
$("#click-me").click(function(){
$("#select-list").empty();
});
The final code and output for this example of how to remove all of the options from a select list on click using jQuery and Javascript is below:
Code Output:
Click Me to Remove All Options from the Select List Below
Full Code:
<div class="html-code-output">
<div id="div">
<p id="click-me">Click Me to Remove All Options from the Select List Below</p>
<select id="select-list">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
</div>
<script>
$("#click-me").click(function(){
$("#select-list").empty();
});
</script>
Using jQuery to Remove All Options from Select and Add Options After
When working with dropdowns and select elements, removing all options is good, but after we typically will want to add options to the select. We can use the jQuery append() method to add options to a select list very easily.
The key to creating options to a select list is calling the append() method multiple times – either in a loop or just consecutively in the code.
So to add options after removing all options, we can use the empty() method, and after use the append() method.
For example, if we want to remove all options from a select and after create 2 options to our select list, we can do so with the following Javascript code:
$("#click-me").click(function(){
$("#select-list").empty();
$("#select-list").append($('<option>', {value:1, text:'Option 1'}));
$("#select-list").append($('<option>', {value:2, text:'Option 2'}));
});
Or, if we have an array of key and value pairs (will be slow if you have a big array), we can do the following in a loop.
var items = [{ value:1, text: 'Option 1' }, { value:2, text: 'Option 2' }];
$("#click-me").click(function(){
$("#select-list").empty();
$.each(items, function(i, item) {
$("#select-list").append($('<option>', {value: item.value, text: item.text}));
});
});
Let’s say we have the following HTML:
<div id="div">
<p id="click-me">Click Me to Add Remove All Options and Add Back Two Options in the Select List Below</p>
<select id="select-list">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
If we want to remove all the options after a click, and then add back two different options, we just need to do the following in our Javascript code:
var items = [{ value:0, text: 'Different Option 0' }, { value:1, text: 'Different Option 1' }];
$("#click-me").click(function(){
$("#select-list").empty();
$.each(items, function(i, item) {
$("#select-list").append($('<option>', {value: item.value, text: item.text}));
});
});
The final code and output for this example of how to remove all options from a select and after add multiple options on click to the select list using jQuery and Javascript is below:
Code Output:
Click Me to Add Remove All Options and Add Back Two Options in the Select List Below
Full Code:
<div class="html-code-output">
<div id="div">
<p id="click-me">Click Me to Add Remove All Options and Add Back Two Options in the Select List Below</p>
<select id="select-list">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
</div>
<script>
var items = [{ value:0, text: 'Different Option 0' }, { value:1, text: 'Different Option 1' }];
$("#click-me").click(function(){
$("#select-list").empty();
$.each(items, function(i, item) {
$("#select-list").append($('<option>', {value: item.value, text: item.text}));
});
});
</script>
Hopefully this article has been useful for you to understand how to use jQuery to remove all options from a select list.
Leave a Reply