Answers for "how to insert selected option into input field"

1

input and select box combined

<input type="text" list="cars" />
<datalist id="cars">
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</datalist>
Posted by: Guest on August-01-2020
1

javascript add option element to select

javascript add option/element to select:
const newOpt = document.getElementById('option-input');
let mySelect = document.getElementById('mySelect'); //Object not array
const opts = Object.values(mySelect.options);
const option = document.createElement('option');
optValues = opts.map(opt => opt.value.toLowerCase());
//Validate if option exists
if(optValues.indexOf(newOpt.value.toLowerCase()) !== -1){
  	newOpt.nextElementSibling.after("Category exists");
   }else{
   option.value = newOpt.value;
    option.text = newOpt.value;
    mySelect.add(option);
    mySelect.value = option.value;
   }
Posted by: Guest on May-25-2020

Code answers related to "how to insert selected option into input field"

Browse Popular Code Answers by Language