How to change selected value of a drop-down list using jQuery?
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:
- 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:

Recommended Posts:
- How to get selected value in dropdown list using JavaScript ?
- How to get selected text from a drop-down list using jQuery?
- jQuery | :selected Selector
- How to get all selected checkboxes in an array using jQuery ?
- How to know which radio button is selected using jQuery?
- jQuery | change() with Examples
- How to change the element id using jQuery ?
- How to change cursor style using jQuery ?
- JQuery | Change the text of a span element
- Content editable change event in jQuery
- How to change the src attribute of an img element in JavaScript / jQuery ?
- How to style a dropdown using CSS?
- How to remove arrow in dropdown in Bootstrap ?
- How to sort a list alphabetically using jQuery ?
- How to avoid dropdown menu to close menu items on clicking inside ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



