The Wayback Machine - https://web.archive.org/web/20240118182718/https://www.geeksforgeeks.org/how-to-reset-selected-value-to-default-using-jquery/
Open In App
Related Articles

How to reset selected value to default using jQuery ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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=
    </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=
    </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.

 


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials