Answers for "dynamic load option of select with javascript"

0

dynamic load option of select with javascript

document.getElementById('generate').onclick = function() {
 
  var values = ["dog", "cat", "parrot", "rabbit"];
 
  var select = document.createElement("select");
  select.name = "pets";
  select.id = "pets"
 
  for (const val of values) {
    var option = document.createElement("option");
    option.value = val;
    option.text = val.charAt(0).toUpperCase() + val.slice(1);
    select.appendChild(option);
  }
 
  var label = document.createElement("label");
  label.innerHTML = "Choose your pets: "
  label.htmlFor = "pets";
 
  document.getElementById("container").appendChild(label).appendChild(select);
}
Posted by: Guest on June-11-2020

Code answers related to "dynamic load option of select with javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language