How do I programmatically set the value of a select box element using JavaScript?
Posted By: brass-kazoo
I have the following HTML <select>
element:
<select id="leaveCode" name="leaveCode">
<option value="10">Annual Leave</option>
<option value="11">Medical Leave</option>
<option value="14">Long Service</option>
<option value="17">Leave Without Pay</option>
</select>
Using a JavaScript function with the leaveCode
number as a parameter, how do I select the appropriate option in the list?
Solution
You can use this function:
selectElement('leaveCode', '11')
function selectElement(id, valueToSelect) {
let element = document.getElementById(id);
element.value = valueToSelect;
}
Answered By: Mitchel Sellers
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.