How to populate the options of a select element in JavaScript?

The task is to add elements to the select element from the JavaScript array. We can also get selected value in dropdown list using JavaScript. Here we will populate the dropdown list with an array. Below is the description of popular approaches used in JavaScript.

Example 1: In this example, the length property is used to traverse the elements of the array and on each element create an option element and append this new element to the select element by appendChild() method. 

  • Program: 

HTML




<!DOCTYPE html>

<html>

 

<head>

    <title>

<1<2

<1<4

    <6title>

<6head>

 

<html3 html4html5html6>

     

    <>1 html4html5>4>

<1>7

    <6>1>

     

    <<5 <6html5<8 html4html5

<1head2>

    <6<5>

 

 

     

    <>1 <6html5>4>5>1>

     

    <    1    2    1>

     

    <    8     9html5<1>

<1<4

    <6    8>

     

    <<5 <6html5title5 title6

<1title8

    <6<5>

 

 

     

    <<06>

<1<09

<1<11

<1<13

<1<15

<1<17

<18<19

<18<21    1<23

 

<1<25

<1<27

<28<29<30<31

<32<33<34 html5<36<37

<32<33<40 html5<42<43

<32<45 html5<34<48

<32<50 html5<34<48

<32<55

<28<57

<28<59 html5<61<48

<1<57

    <66

<6html3>

 

<6html>

  • Output: 

How to populate the options of a select element in JavaScript?

Example 2: In this example, the each() method is used to traverse the elements of the array and on each element create an option element and append this new element to the select element by append() method in JQuery.  

  • Program: 

HTML




<!DOCTYPE html>

<html>

 

<head>

    <title>

<1<2

<1<4

    <6title>

     

    <<06 <96html5

<98>

    <6<06>

<6head>

 

<html3 html4html5html6>

     

    <>1 html4html5>4>

<1>7

    <6>1>

     

    <<5 <6html5<8 html4html5

<1html37>

    <6<5>

 

 

     

    <>1 <6html5>4>5>1>

     

    <    1    2    1>

     

    <    8     9html5<1>

<1<4

    <6    8>

     

    <<5 <6html5title5 title6

<1html83

    <6<5>

 

 

     

    <<06>

<1<09

<1<11

<1<13

<1<15

<1<17

<32<19

<32<21    1<23

 

<1<25

<1<27

<28>14

<32>16>17>5>17>20

>21>22

<28>24

<28>26

<1<57

    <6<06>

<6html3>

 

<6html>

  • Output:

How to populate the options of a select element in JavaScript?

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

How to get data from select option in JavaScript?

The val() method returns the value of selected attribute value. let selectedValue = $("#selectVal option:selected"). val(); The text() method returns a string of the selected option.

How to add options to a select element using JavaScript?

Summary.
JavaScript uses the HTMLSelectElement type to represent the <select> element..
Use the add() method of the HTMLSelectElement to add an option to the <select> element..
Use the remove() method of the HTMLSelectElement to remove an option from the <select> element..

How to add select options dynamically using JavaScript?

Add Options Dynamically To Select Element Inside the loop, create an option element and store in the variable called option. Then, set a value attribute to the option element and assign its value to the value of the property from the countriesData object on each iteration.

How do you populate a select?

To populate select list with jQuery, use for loop along with append() and append the options to the already created select.