To add an option to a select list using jQuery, the simplest way is to use the append() method.
$("#select-list").append($('
Let’s say I have the following HTML:
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($('
The resulting HTML would be as follows:
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#select-list").append(jQuery('
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.
Click Me to Add an Option to the Select List Below
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($('
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:
Click Me to Add an Option to the Select List Below
<script>
$("#click-me").click(function(){
$("#select-list").append($('
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($('
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($('
Let’s say we have the following HTML:
Click Me to Add Multiple Options to the Select List Below
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($('
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:
Click Me to Add Multiple Options to the Select List Below
<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($('
Hopefully this article has been useful for you to understand how to use jQuery to add multiple options to a select list HTML element.