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.
- 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.
Example 2: This example uses attr() function to assign the selected attribute to the corresponding option.
- How to reset selected value to default using jQuery ?
- How to change dropdown menu when time is changed ?
- How to display div elements using Dropdown Menu in jQuery?
- How to get selected text from a drop-down list using jQuery?
- How to change the color of selected text using CSS ?
- How to know which radio button is selected using jQuery?
- How to get all selected checkboxes in an array using jQuery ?
- How to get file input by selected file name without path using jQuery ?
- How to show contents of selected row of a table in Bootstrap model using jQuery ?
- How to remove the default arrow icon from a dropdown list?
- How to Add Image into Dropdown List for each items ?
- jQuery | :selected Selector
- How to change the checkbox value using jQuery ?
- How to style a dropdown using CSS?
- How to design full width dropdown Navbar using Bootstrap ?
- How to detect value change on hidden input field in JQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.