set selected option javascript code example
Example 1: javascript get selected option
var e = document.getElementById("selectElementID");
var value=e.selectElement.options[e.selectedIndex].value;
var text=e.options[e.selectedIndex].text;
Example 2: make select option selected javascript
document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected'
Example 3: javascript select change selected
const options = Array.from(select.options);
options.forEach((option, i) => {
if (option.value === use_id) select.selectedIndex = i;
});
Example 4: javascript set html select value
var select = document.getElementById('<your select element id>');
select.value = "1";
Example 5: js select option in form
<form name="AddAndEdit">
<select name="list" id="personlist">
<option value="P1">Person1</option>
<option value="P2">Person2</option>
<option value="P3">Person3</option>
</select>
</form>
Example 6: javascript set select option
document.getElementById("mySelectID").value="my_value";