To revert the select option to its initially selected default, you can reset the value of the select element using JavaScript.
Using JavaScript, you can access the select element by its id, and then set its value to the default value using the defaultValue property.
Here is an example code snippet:
HTML:
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3" selected>Option 3</option>
</select>
<button onclick="resetSelect()">Reset</button>
JavaScript:
function resetSelect() {
document.getElementById("mySelect").value = document.getElementById("mySelect").defaultValue;
}
In this example, the select element has three options, with the third option initially selected. When the Reset button is clicked, the resetSelect() function is called, which sets the select element's value to its default value, which in this case is the value of the initially selected option (Option 3). This will revert the select option to its initially selected default.
Asked: 2021-06-17 11:00:00 +0000
Seen: 10 times
Last updated: Apr 06 '22