Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to change selected value of a drop-down list using jQuery?

  • Difficulty Level : Medium
  • Last Updated : 03 Aug, 2021

With jQuery, it is easy to writing one line of code to change the selected value from a drop-down list. Suppose, you have a select element and you need to select one of its options based on one of its values. To achieve this feat you can use various methods, two of those methods will be explained below.

Used Function:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • val() function: It sets or returns the value attribute of the selected elements.
  • attr() function: It sets or returns the attributes and values of the selected elements.

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




<!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=
    </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:



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




<!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=
    </script>
      
    <script type="text/javascript">
        $(document).ready(()=>{
            $("#select option[value=3]").attr('selected', 'selected');
        });  
    </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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!