Languages
[Edit]
EN

JavaScript - how to add options (items) to select (ComboBox) element

3 points
Created by:
Root-ssh
178170

In this article, we're going to have a look at how to add new options (items) to select (ComboBox) element in JavaScript. Solutions for this problems can be divided into 2 groups:

  • that use option element API (example 1 and 2),
  • that use DOM element API (example 3 and 4).

Look on below examples to see how they work:

1. add method example

Edit

Approach presented in this section uses add method available for each select element.

2. options property with add method example

Edit

In this section we use options property that keeps all items in select element.

3. appendChild method example

Edit

In this section we use appendChild method that append child elements to select element - used typical DOM method.

4. innerHTML property example

Edit

In this section we use innerHTML property that in presented case overrides select element content - used typical DOM proeprty.

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join