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.
- 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.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.