Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset all select dropdowns

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>
like image 862
Keith Power Avatar asked Jan 03 '12 11:01

Keith Power


People also ask

How do I reset my drop down options?

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).

How do you reset the selected DropDown value in react?

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.

How do I remove a DropDownList in HTML?

Select remove() Method The remove() method is used to remove an option from a drop-down list.

How do you empty a select in JavaScript?

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.


2 Answers

Here´s a way to select the first option in all selects 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.

like image 148
Stefan Avatar answered Nov 06 '22 19:11

Stefan


function displaymessage() {
    $("select").each(function() { this.selectedIndex = 0 });
}

this selects the first option (not necessarily having value = 0)

like image 30
Fabrizio Calderan Avatar answered Nov 06 '22 19:11

Fabrizio Calderan