brass-kazoo brass-kazoo - 1 month ago 5
Javascript Question

How do I programmatically set the value of a select box element using javascript?

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?

Answer
function SelectElement(valueToSelect)
{    
    var element = document.getElementById('leaveCode');
    element.value = valueToSelect;
}