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:
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:
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:
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.
If you forget to clear the old options, every run adds duplicate entries.
Selecting a value after appending
Set the selected value with .val():
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:
Why not append an HTML string
You can append a raw string:
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.
Related reading
- Adding to an array asynchronously in Node.js
- Ajax request returns 200 OK, but an error event is fired instead of success
- Algorithm to determine the points delimiting the boundaries of a shape -- using javascript
- Amazon Elastic Beanstalk node and npm non-standard install locations
- amqp vs amqplib - which Node.js amqp client library is better?
- An invalid form control with name='' is not focusable
- Android Calling JavaScript functions in WebView
- Android webview localStorage
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.