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 select values from a JSON object using jQuery ?
Next article icon

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

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 dropdown list using jQuery:

Table of Content

  • By using the val() method
  • By using prop() method
  • By using the attr() method

By using the val() method

The val() method can be used by specifying a value attribute with some default values inside the option tags of the select element. Once you set the value attribute, you can now pass the value of the particular element that you want to replace with the default shown value as a parameter to the val() method in the form of a string.

Syntax:

$('element_selector').val('value');

Example: This example uses val() function to set the option corresponding to the passed value.

html
<!DOCTYPE html>  <html>       <head>      <title>          Change selected value          of a drop-down list      </title>           <style>          div {              width:15%;              background-color:green;              padding:8px;              color:azure;          }      </style>           <script src=  "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">      </script>           <script type="text/javascript">          $(document).ready(()=>{              $("#select").val('2');          });          </script>  </head>   <body>      <div>          <p id="gfg">GeeksforGeeks courses:</p>           <select id="select">                                                          <option value="0">System Design</option>              <option value="1">DSA-Online</option>              <option value="2">Fork Python</option>              <option value="3">Fork Java</option>          </select>      </div>  </body>   </html>                      

Output:

By using prop() method

You can also use the prop() method to change the value of a dropdown list. You can select the particular element using th element selector and use the selected property and set it to true.

Syntax:

$('element_selector').prop('selected', true);

Example: The below example uses the prop() method to change the value of the dropdown list:

HTML
<!DOCTYPE html>  <html>       <head>      <title>          Change selected value          of a drop-down list      </title>           <style>          div {              width:15%;              background-color:green;              padding:8px;              color:azure;          }      </style>           <script src=  "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">      </script>           <script type="text/javascript">          $(document).ready(()=>{              $("#select option[value=1]").prop('selected', true);          });          </script>  </head>   <body>      <center>         <div>              <p id="gfg">GeeksforGeeks courses:</p>              <select id="select">                                                              <option value="0">System Design</option>                  <option value="1">DSA-Online</option>                  <option value="2">Fork Python</option>                  <option value="3">Fork Java</option>              </select>          </div>     </center>  </body>   </html>                      

Output:

dropDownOut

By using the attr() method

The attr() method can also be used to change the value of the dropdown list. You have to select the element and use the attr() method with selected attribute and set it to true to replace the value of dropdown with the selected value of the selected element.

Syntax:

$('selected_element').attr('selected', true)

Example: This example uses attr() function to assign the selected attribute to the corresponding option.

html
<!DOCTYPE html>  <html>       <head>      <title>          Change selected value          of a drop-down list      </title>           <style>          div {              width:15%;              background-color:green;              padding:8px;              color:azure;          }      </style>           <script src=  "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">      </script>           <script type="text/javascript">          $(document).ready(()=>{              $("#select option[value=3]").attr('selected', true);          });      </script>  </head>   <body>      <div>          <p id="gfg">GeeksforGeeks courses:</p>           <select id="select">                                                          <option value="0">System Design</option>              <option value="1">DSA-Online</option>              <option value="2">Fork Python</option>              <option value="3">Fork Java</option>          </select>      </div>  </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.



Next Article
How to select values from a JSON object using jQuery ?

A

AnkitMishra16
Improve
Article Tags :
  • JavaScript
  • JQuery
  • Web Technologies
  • jQuery-Misc

Similar Reads

  • 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 add options to a drop-down list using jQuery?
    Given an HTML document with a drop-down list menu and our task is to add more options to the drop-down list using jQuery. Approach : To add more option to the existing drop-down list using the append() method : var options = { val1: 'C#', val2: 'PHP' }; var selectOption = $('#programmingLanguage');
    2 min read
  • How to select values from a JSON object using jQuery ?
    In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. This append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).appen
    2 min read
  • How to reset selected value to default using jQuery?
    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
    3 min read
  • How to change the checkbox value using jQuery ?
    The Checkboxes are used to let a user select one or more options for a limited number of choices. The :checkbox selector selects input elements with type checkbox.Syntax: $('#textboxID').val($("#checkboxID").is(':checked')); In the above syntax, basically the return value of the checked or unchecked
    2 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 change font style using drop-down list in JavaScript ?
    To change or set a font style for certain text, the fontFamily CSS property needs to be changed. The fontFamily property sets or returns a list of font-family names for text in an element. Syntax: object.style.fontFamily = "font" To change the font style by option dropdown: The font values can be pa
    2 min read
  • How to select first element in the drop-down list using jQuery ?
    Given a drop-down list containing options elements, the task is to get the first element of the select element using JQuery. Approach 1: Select the first element of <select> element using the jQuery selector.Use the prop() method to get access to properties of that particular element.Change th
    2 min read
  • How to Change Font Size using drop-down List in JavaScript ?
    To change or set a font size for certain text, the fontSize property needs to be changed. The fontSize property sets or returns a list of fontSize names for text in an element. CSS font-size property is used to specify the font's height and size. It affects the size of an element's text. It has a me
    3 min read
  • How to create a Mini sized 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 creating a Mini sized selects using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel="sty
    1 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