I have have a function that gets called by onbeforeunload. Within I wish to reset all the dropdowns but I cannot find the right method to reset them back to the 0 value.
<head>
<script type="text/javascript">
function displaymessage() {
document.getElementsByTagName('select').value = 1;
//or
document.getElementsByTagName('select').options.length = 0;
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
<select name="data[Rate][15][12][num_rooms]" id="r15ro12">
<option value="0">0</option><option value="1">1 Rooms</option>
<option value="2">2 Rooms</option><option value="3">3 Rooms</option><option value="4">4 Rooms</option>
<option value="5">5 Rooms</option><option value="6">6 Rooms</option><option value="7">7 Rooms</option>
<option value="8">8 Rooms</option><option value="9">9 Rooms</option>
</select>
<select name="data[Rate][15][12][num_rooms]" id="r15ro12">
<option value="0">0</option><option value="1">1 Rooms</option>
<option value="2">2 Rooms</option><option value="3">3 Rooms</option><option value="4">4 Rooms</option>
<option value="5">5 Rooms</option><option value="6">6 Rooms</option><option value="7">7 Rooms</option>
<option value="8">8 Rooms</option><option value="9">9 Rooms</option>
</select>
</body>
</html>
The following HTML Markup consists of an HTML DropDownList (DropDown) control and a Button. When the Button is clicked, the Reset JavaScript function is executed. Inside this function, the SelectedIndex property of the DropDownList is set to 0 (First Item).
By clicking on the clear icon which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction. By using showClearButton property, you can enable the clear icon in DropDownList element.
Select remove() Method The remove() method is used to remove an option from a drop-down list.
To remove the options of an HTML element of select , you can utilize the remove() method: function removeOptions(selectElement) { var i, L = selectElement. options. length - 1; for(i = L; i >= 0; i--) { selectElement.
Here´s a way to select the first option in all select
s using native JavaScript;
var elements = document.getElementsByTagName('select');
for (var i = 0; i < elements.length; i++)
{
elements[i].selectedIndex = 0;
}
...but jQuery is very handy and makes most things much easier.
function displaymessage() {
$("select").each(function() { this.selectedIndex = 0 });
}
this selects the first option (not necessarily having value = 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With