Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to change Selected Value of a Drop-Down List using jQuery?
Next article icon

How to reset selected value to default using jQuery?

Last Updated : 05 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Resetting a selected value to its default in a dropdown or select element is a common requirement in web development to enhance user experience. jQuery, a popular JavaScript library, simplifies this task by providing easy-to-use methods for manipulating DOM elements. By using jQuery, you can quickly reset the selected value of a dropdown to its initial default option, ensuring that the user interface remains intuitive and user-friendly. This guide will walk you through the steps to reset the selected value of a dropdown to its default using jQuery, making it a straightforward process for any web developer.

These are the following approaches:

Table of Content

  • Using prop() method
  • Using each() method

Using prop() method

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: This example shows the use of prop() method to reset the selected value to the default value.

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>         let el_up = document.getElementById('GFG_UP');         let 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: 

https://media.geeksforgeeks.org/wp-content/uploads/20240802172238/Untitled100.mp4

Using each() method

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: This example shows the use of each() method to reset the selected value to the default value.

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>         let el_up = document.getElementById('GFG_UP');         let 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: 

https://media.geeksforgeeks.org/wp-content/uploads/20240802172407/Untitled101.mp4


Next Article
How to change Selected Value of a Drop-Down List using jQuery?

P

PranchalKatiyar
Improve
Article Tags :
  • CSS
  • HTML
  • JavaScript
  • JQuery
  • Web Technologies
  • CSS-Misc
  • HTML-Misc
  • HTML-Questions
  • JavaScript-Questions
  • jQuery-Misc
  • jQuery-Questions

Similar Reads

  • How to change Selected Value of a Drop-Down List using jQuery?
    We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
    3 min read
  • How to create Selected Option Select using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a Selected Option Select using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=
    1 min read
  • How to get text value of a selected option in jQuery ?
    Introduction: In this article, we will see how we can use jQuery to get the text value from the selected option. While working on some form-related user interface, we often use the select option, and to use that, we will surely need those values that are being selected. Approach: We will use select
    2 min read
  • How to get selected text from a drop-down list using jQuery?
    In this article, we will learn how we can get the text of the selected option in a select tag using jQuery. There are two ways in jQuery that we can use to accomplish this task. Table of Content Using the val() method By using option:selected selector and text() method togehterUsing the val() method
    2 min read
  • How to set attribute without value using jQuery ?
    There are many ways to set attributes without value using jQuery. jQuery prop() method is used to get or set the values of attributes. To change the properties like checked, default checked, disabled, selectedIndex, default selected and selected state of Document Object Model, use the jQuery .prop()
    3 min read
  • How to Get Selected Value in Dropdown List using JavaScript?
    A dropdown list is an HTML element that allows users to select one option from a list of options. The <select> and <option> tags are used to create dropdown lists in HTML. In this article, we will see how to get selected value from dropdown list using JavaScript. Basic HTML Structure for
    3 min read
  • How to Reset Form using jQuery with reset() Method?
    Reset a form means clear all input fields data and returns to their initial default values. It can be useful where a user wants to clear all entered data or you want to reset the form after a successful submission. The reset() method can easily reset the form data after clicking the reset button or
    3 min read
  • How to remove options from select element using jQuery ?
    Removing options from a select element using jQuery means programmatically deleting specific or all <option> elements within a <select> dropdown. This can be done using jQuery methods like remove() or empty(), allowing dynamic updates of the dropdown's available choices based on conditio
    3 min read
  • How to make Basic selects using jQuery Mobile ?
    jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Basic selects using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="stylesheet" h
    1 min read
  • How to Get the Value of an Input Text Box using jQuery?
    When working with forms or interactive web pages, sometimes need to get the values entered into the input fields. This article provide a complete guide on how to get the value o an input text box using jQuery. Below are the different approaches to get the value of an input text box using jQuery: Get
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences