• 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 Add Option to Select List

Using jQuery to Add Option to Select List

November 30, 2021 Leave a Comment

To add an option to a select list using jQuery, the simplest way is to use the append() method.

$("#select-list").append($('<option>', {value:1, text:'new-option-text'}));

Let’s say I have the following HTML:

<div id="div">
    <select id="select-list">
        <option value="0">Option 0</option>
    </select>
</div>

If we want to add an option to the select list, we can use the jQuery append() method to do this with the following Javascript code.

$("#select-list").append($('<option>', {value:1, text:'Option 1'}));

The resulting HTML would be as follows:

<div id="div">
    <select id="select-list">
        <option value="0">Option 0</option>
        <option value="1">Option 1</option>
    </select>
</div>

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#select-list").append(jQuery('<option>', {value:1, text:'Option 1'}));

Adding an Option to a Select List Using jQuery With a Click

Many times when creating a web page and the user experience, we want to add options to select lists based on an interaction with another element on the web page.

We can add options to select lists using jQuery very easily by combining the append() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to add an option.

<div id="div">
    <p id="click-me">Click Me to Add an Option to the Select List Below</p>
    <select id="select-list">
        <option value="0">Option 0</option>
    </select>
</div>

We can utilize both the jQuery click() method

and jQuery append() method to append an option to our select HTML element.

Below is the Javascript code which will allow the user to be able to add an option to our select element:

$("#click-me").click(function(){
    $("#select-list").append($('<option>', {value:1, text:'Option 1'}));
}); 

The final code and output for this example of how to add an option to a select list on click using jQuery and Javascript is below:

Code Output:

Click Me to Add an Option to the Select List Below

Full Code:


<div class="html-code-output">
<div id="div">
    <p id="click-me">Click Me to Add an Option to the Select List Below</p>
    <select id="select-list">
        <option value="0">Option 0</option>
    </select>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#select-list").append($('<option>', {value:1, text:'Option 1'}));
});

</script>

Using jQuery to Add Multiple Options to Select List

We can use the jQuery append() method to add multiple options to a select list very easily.

The key to creating multiple options to a select list is calling the append() method multiple times – either in a loop or just consecutively in the code.

For example, if we want to create 2 options to our select list, we can do so with the following Javascript code:

$("#click-me").click(function(){
    $("#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(){
    $.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 Multiple Options to the Select List Below</p>
    <select id="select-list">
        <option value="0">Option 0</option>
    </select>
</div>

If we want to add multiple options after a click, we just need to do the following in our Javascript code:

var items = [{ value:1, text: 'Option 1' }, { value:2, text: 'Option 2' }];

$("#click-me").click(function(){
    $.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 add multiple options on click to a select list using jQuery and Javascript is below:

Code Output:

Click Me to Add Multiple Options to the Select List Below

Full Code:


<div class="html-code-output">
<div id="div">
    <p id="click-me">Click Me to Add Multiple Options to the Select List Below</p>
    <select id="select-list">
        <option value="0">Option 0</option>
    </select>
</div>
</div>

<script>

var items = [{ value:1, text: 'Option 1' }, { value:2, text: 'Option 2' }];

$("#click-me").click(function(){
    $.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 add multiple options to a select list HTML element.

Other Articles You'll Also Like:

  • 1.  jQuery Random Number Generator Between Two Numbers
  • 2.  How to Check if Element Exists Using jQuery
  • 3.  Using jQuery to Change Text of HTML Element
  • 4.  Using jQuery to Get Value of Select On Change
  • 5.  jQuery prependTo – Insert Element As First Child
  • 6.  Using jQuery to Set Select to the First Option
  • 7.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 8.  Set the Height of a Div Using jQuery
  • 9.  How to Uncheck All Checkboxes in jQuery
  • 10.  jQuery mouseenter – An Interactive Example of the mouseenter Method

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