Programmatically Setting Select Box Value using JavaScript
To set the value of a <select> element based on a parameter, utilize the following JavaScript function:
function selectElement(id, valueToSelect) { let element = document.getElementById(id); element.value = valueToSelect; }
Consider the following HTML <select> element:
<select>
To select the "Medical Leave" option (value "11"), call the function as follows:
selectElement('leaveCode', '11');
This will programmatically set the value of the <select> element to "11".
The above is the detailed content of How to Programmatically Set a Select Box Value in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!