How to reset selected value to default using jQuery ? - GeeksforGeeks

23 Aug, 2021
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:  
html
<!DOCTYPE HTML><html> <head>    <title>        Reset select value to        default with JQuery    </title>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </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>
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:  
html
<!DOCTYPE HTML><html> <head>    <title>        Reset select value to        default with JQuery    </title>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </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>
Output:  
 
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”. You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
 
surinderdawra388
CSS-Misc
HTML-Misc
jQuery-Misc
CSS
HTML
JavaScript
JQuery
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to apply style to parent if it has child with CSS?
How to set space between the flexbox ?
Design a web page using HTML and CSS
Create a Responsive Navbar using ReactJS
Form validation using jQuery
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
HTML | <img> align Attribute