• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Remove All Options From Select

Using jQuery to Remove All Options From Select

December 12, 2021 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Remove Attribute from HTML Element
  • 2.  Using jQuery to Get the Bottom Position of Element
  • 3.  Using jQuery to Get the Previous Sibling of an Element
  • 4.  How to Use jQuery to Remove Element
  • 5.  jQuery insertBefore – Insert Element Before Another Element
  • 6.  jQuery blur – How to Use the blur() Method on an HTML Form
  • 7.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 8.  onclick this jQuery
  • 9.  jQuery rotate – How to Rotate an Image using jQuery
  • 10.  Using jQuery to Toggle Class of HTML Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy