jquery selected option value

How to Get the Selected Option Value Using jQuery

If you are working with HTML forms, you may need to get the selected value of a dropdown list using jQuery. This can be achieved using the .val() method.

Example:

Assuming you have a dropdown list with ID "myDropdown":


      <select id="myDropdown">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
      </select>
    

You can get the selected value using the following jQuery code:


      var selectedValue = $('#myDropdown').val();
    

The variable selectedValue will now contain the value of the selected option, which in this case could be "option1", "option2" or "option3".

Alternatively, you can also use the :selected selector to get the selected option:


      var selectedValue = $('#myDropdown option:selected').val();
    

Both methods will return the same result.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe