jQuery
Web Development
HTML
Programming
JavaScript

Adding options to a select element using jQuery

Codemia Knowledge Hub ·

To add an option to a dropdown with jQuery, create a real option element, set its value and text, and append it to the target select with .append().

Start with a normal dropdown:

html
<select id="countrySelect">
  <option value="">Choose a country</option>
</select>

Once the page has loaded, jQuery can find this element and add options to it.

Adding one option

Create an option node with jQuery("<option>") and append it:

html
1<select id="countrySelect">
2  <option value="">Choose a country</option>
3</select>
4
5<script>
6  $("#countrySelect").append(
7    $("<option>", {
8      value: "ca",
9      text: "Canada"
10    })
11  );
12</script>

There is no string concatenation, and it is obvious which part is the value and which is the display label.

Adding several options from an array

Options usually come from an array or an API response:

html
1<select id="countrySelect"></select>
2
3<script>
4  const countries = [
5    { value: "ca", text: "Canada" },
6    { value: "us", text: "United States" },
7    { value: "mx", text: "Mexico" }
8  ];
9
10  const $select = $("#countrySelect");
11
12  $.each(countries, function (_, item) {
13    $select.append(
14      $("<option>", {
15        value: item.value,
16        text: item.text
17      })
18    );
19  });
20</script>

This keeps the dropdown structure separate from the data source.

Replacing existing options

To rebuild the list, clear it with .empty() and then add the new options. This is common when a second dropdown depends on the first one's selection.

html
1<select id="citySelect">
2  <option value="">Old value</option>
3</select>
4
5<script>
6  const cities = [
7    { value: "tor", text: "Toronto" },
8    { value: "van", text: "Vancouver" }
9  ];
10
11  const $citySelect = $("#citySelect");
12  $citySelect.empty();
13  $citySelect.append($("<option>", { value: "", text: "Choose a city" }));
14
15  $.each(cities, function (_, city) {
16    $citySelect.append(
17      $("<option>", {
18        value: city.value,
19        text: city.text
20      })
21    );
22  });
23</script>

If you forget to clear the old options, every run adds duplicate entries.

Selecting a value after appending

Set the selected value with .val():

javascript
$("#countrySelect").val("us");

This works only if an option with that value already exists. If it does not, the selection stays unchanged, and it will not update itself when the option is added later. Append first, then select.

Populating from an AJAX response

The same append logic goes inside the success handler, so the dropdown is updated after the data arrives:

javascript
1$.getJSON("/api/countries", function (countries) {
2  const $select = $("#countrySelect");
3  $select.empty();
4  $select.append($("<option>", { value: "", text: "Choose a country" }));
5
6  $.each(countries, function (_, item) {
7    $select.append(
8      $("<option>", {
9        value: item.code,
10        text: item.name
11      })
12    );
13  });
14});

Why not append an HTML string

You can append a raw string:

javascript
$("#countrySelect").append('<option value="ca">Canada</option>');

It works, but building elements from an object is easier to read and less error-prone when values come from variables, and it avoids malformed markup from string-building mistakes.

When nothing appears

Check the selector. If $("#countrySelect") matches nothing, jQuery does not throw an error, and the dropdown simply stays unchanged.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms