Adding option to html selectbox using JavaScript

Posted on 13-03-2017 by Nadir Latif


Source: https://www.w3schools.com/jsref/met_select_add.asp

In order to add an html option to a selectbox using JavaScript we can use following code:


var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Option Text";
option.value = "Option Value";
x.add(option);