How to Create a Dropdown List with Array Values using 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 populer 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:
<!DOCTYPE html><html><head><title>Populate dropdown listwith array values</title></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksforGeeks</h1><pid="geeks"style="font-size:20px; font-weight:bold"></p><selectid="arr"></select><br><br><buttononclick="GFG_Fun();">click here</button><pid="gfg"style="font-size: 26px;font-weight: bold;color: green;"></p><script>var up = document.getElementById('geeks');var down = document.getElementById('gfg');var select = document.getElementById("arr");var elmts = ["HTML", "CSS", "JS", "PHP", "jQuery"];up.innerHTML = "Click on the button to "+ "perform the operation"+".<br>Array - [" + elmts + "]";// Main functionfunction GFG_Fun() {for (var i = 0; i <elmts.length; i++) {varoptn=elmts[i];varel=document.createElement("option");el.textContent=optn;el.value=optn;select.appendChild(el);}down.innerHTML="Elements Added";}</script></body></html>chevron_rightfilter_none - Output:

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:
<!DOCTYPE html><html><head><title>Populate dropdown listwith array values</title><scriptsrc=</script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksforGeeks</h1><pid="geeks"style="font-size: 20px;font-weight: bold"></p><selectid="arr"></select><br><br><buttononclick="GFG_Fun();">click here</button><pid="gfg"style="font-size: 26px;font-weight: bold; color: green;"></p><script>var up = document.getElementById('geeks');var down = document.getElementById('gfg');var select = document.getElementById("arr");var elmts = ["HTML", "CSS", "JS", "PHP", "jQuery"];up.innerHTML = "Click on the button to "+ "perform the operation"+".<br>Array - [" + elmts + "]";// Main functionfunction GFG_Fun() {$.each(elmts, function(i, p) {$('#arr').append($('<option></option>').val(p).html(p));});down.innerHTML = "Elements Added";}</script></body></html>chevron_rightfilter_none - Output:

Recommended Posts:
- How to create an array with random values with the help of JavaScript ?
- How to get selected value in dropdown list using JavaScript ?
- Create a comma separated list from an array in JavaScript
- JavaScript | array.values()
- JavaScript | Get all non-unique values from an array
- Find all the combinations of the array values in JavaScript
- How to get values from html input array using JavaScript ?
- How to check all values of an array are equal or not in JavaScript ?
- How to create two dimensional array in JavaScript?
- How to remove the default arrow icon from a dropdown list?
- How to get all unique values (remove duplicates) in a JavaScript array?
- How to clear previous appended data on change the dropdown menu in JavaScript ?
- How to create comma separated list from an array in PHP ?
- How to get a list of associative array keys in JavaScript ?
- Object.values( ) In JavaScript
- JavaScript | typedArray.values() with Examples
- How to read CSS rule values with JavaScript?
- Object.create( ) In JavaScript
- How to create a link in JavaScript ?
- How to create a style tag using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


