js add element to select code example
Example: javascript add option element to select
javascript add option/element to select:
const newOpt = document.getElementById('option-input');
let mySelect = document.getElementById('mySelect');
const opts = Object.values(mySelect.options);
const option = document.createElement('option');
optValues = opts.map(opt => opt.value.toLowerCase());
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;
}