How to reset selected value to default using jQuery ?

The task is to reset the value of the select element to its default value with the help of jQuery. There are two approaches that are discussed below:

Approach 1: First select the options using jQuery selectors then use prop() method to get access to its properties. If the property is selected then return the default value by using defaultSelected property.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Reset select value to
            default with JQeury
        </title>
      
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <select id="select">
            <option value="GFG_1">GFG_1</option>
            <option value="GFG_2" selected="selected">
                GFG_2
            </option>
            <option value="GFG_3">GFG_3</option>
        </select>
      
        <button onclick="GFG_Fun()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
      
        <script>
            var el_up = document.getElementById('GFG_UP');
            var el_down = document.getElementById('GFG_DOWN');
            el_up.innerHTML = "Select a different option "
                    + "and then click on the button to "
                    + "perform the operation";
                      
            function GFG_Fun() {
                $('#select option').prop('selected', function () {
                    return this.defaultSelected;
                });
                el_down.innerHTML = "Default selected";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

Approach 2: First, select the options using jQuery selectors then use each() method to get access to its properties. If the property is selected then return the default value by defaultSelected property.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Reset select value to
            default with JQeury
        </title>
      
        <script src=
        </script>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <select id="select">
            <option value="GFG_1">GFG_1</option>
            <option value="GFG_2" selected="selected">
                GFG_2
            </option>
            <option value="GFG_3">GFG_3</option>
        </select>
      
        <button onclick="GFG_Fun()">
            Click Here
        </button>
      
        <p id="GFG_DOWN"></p>
      
        <script>
            var el_up = document.getElementById('GFG_UP');
            var el_down = document.getElementById('GFG_DOWN');
            el_up.innerHTML = "Select a different option "
                    + "and then click on the button to "
                    + "perform the operation";
                      
            function GFG_Fun() {
                $('#select option').each(function () {
                    if (this.defaultSelected) {
                        this.selected = true;
                        return false;
                    }
                });
                el_down.innerHTML = "Default selected";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.