Add Options to a Select List with JavaScript

Adding options to a select list with JavaScript

JavaScript can add <option> elements to a <select> at runtime. A concise modern pattern is to create an Option object and add it to the select.

const monthList = document.getElementById("monthList");
monthList.add(new Option("January", "January"));

This keeps the original tutorial purpose while avoiding old global form references. You can also remove options or move options between listboxes.

Demo: populate a month list Demo: select current month

Table of Contents

Add One Option Top ↑

function addOption(select, text, value) {
  select.add(new Option(text, value));
}

addOption(monthList, "January", "January");

Add Options from an Array Top ↑

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const monthList = document.getElementById("monthList");

months.forEach((month) => {
  monthList.add(new Option(month, month));
});
<label for="monthList">Month</label>
<select id="monthList" name="month">
  <option value="">Month list</option>
</select>

Select One Option by Default Top ↑

After adding the options, assign the select’s value. To select the current month, getMonth() returns an index from 0 to 11.

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const currentMonth = months[new Date().getMonth()];
monthList.value = currentMonth;

See JavaScript getMonth() and the date selection projects for related date examples.

createElement() Alternative Top ↑

const option = document.createElement("option");
option.value = "January";
option.textContent = "January";
monthList.append(option);

new Option() is convenient for select options; document.createElement() is the general DOM approach. See createElement().

Adding Options to a Datalist Top ↑

A <datalist> provides suggestions while still allowing other input. See the JavaScript datalist example and HTML datalist basics.

Remove options · Move options between lists · Select change event · JavaScript validation




Subscribe to our YouTube Channel here



plus2net.com







Suresh

28-01-2010

"selectbox.options.add(optn);" is not working in IE...
smo

28-01-2010

Check the Demo link. It is working fine in IE, FireFox n Chrome
Prasannjit

08-06-2010

how can we give link on add more option if we want to add more text box?
Raza

23-06-2010

For adding more items from text box: var NewItem = document.FormName.AddMoreTextBox.value; var optn = document.createElement("OPTION"); optn.text = NewItem; optn.value = NewItem; document.PlotForm.ScripName.options.add(optn);
rajesh

10-07-2010

Please help me for my query. I have 5 text box(option A to option E) and one dropdown list. in that im showing in listitem " A to E". in my case wat i need is. if user enter values only in Textbox A and textbox B then dropdwonlist listitem should show only option A&B. same if textbox A,B,C,D has value and E textbox empty. then in dropdownlist it should not get display E. i think it is only possible thru javascript. pls help... Thanks in advance
Ramesh Agye

06-09-2010

Hi, sir i want when i select jan and hit submit button then open a new htm page for jan same nest... it is possible Ramesh Agye
Mikko

06-05-2011

How can I add names to the options?
Santhosh K S

23-04-2012

i have dropdownlist having some filenames,and i have textbox, when i enter "Ab" in textbox,all file names started with Ab should list in same dropdown list useing javascript in client side, please help me
Jim

11-05-2012

Excellent. Simple, with a demo. Just what I needed. Thank you!
Devi

02-01-2013

that totally works...simple , nice and easy
safina

05-06-2014

in function addOption(selectbox,text,value ), can this selectbox be an id of a div for dropdown
soumita

07-10-2014

if i am populating a dropdown list based on selection from another list, then how can i avoid duplication of options on the 2nd list, if we select the same option from the ist list again?
please reply.thank you.
Laskshmisaha

30-06-2016

hiii,am noob to php, so i need a clear example code for three linked dropdown list....
Php Expert Programmer

05-06-2017

Thanks sir,
Worked for me first time. Saved me a lot of work.
Thanks again.



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer