We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery.
By using val() method :
The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
Syntax :
$(selector).val(parameter)
Parameter : parameter is optional.
Example :
<html> <head> <title>jQuery Selector</title> <script src= </script> <script> $(document).ready(function() { $("#submit").click(function() { alert($("#myselection").val()); }); }); </script> <style> div { width: 50%; height: 200px; padding: 10px; border: 2px solid green; } </style> </head> <body> <div> <p>The selected value:</p> <select id="myselection"> <option value="1">First</option> <option value="2">Second</option> </select> <br> <br> <button id="submit">Submit</button> </div> </body> </html> |
Output :
Before click on the button.

After click on the button.

By using option:selected :
The option:selected method is a way in jQuery which is used to return the selected element from a list of the element.
Syntax :
$("#selector option:selected");
Parameter : No parameter required.
Example :
<html> <head> <title>jQuery Selector</title> <script src= </script> <script> $(document).ready(function() { $("#submit").click(function() { var value = $("#myselection option:selected"); alert(value.text()); }); }); </script> <style> div { width: 50%; height: 200px; padding: 10px; border: 2px solid green; } </style> </head> <body> <div> <p>The selected value:</p> <select id="myselection"> <option value="1">First</option> <option value="2">Second</option> </select> <br> <br> <button id="submit">Submit</button> </div> </body> </html> |
Output :
Before click on the button.

After click on the button.

Recommended Posts:
- How to get selected value in dropdown list using JavaScript ?
- 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 get the Highlighted/Selected text in JavaScript?
- How to change selected value of a drop-down list using jQuery?
- How to display div elements using Dropdown Menu in jQuery?
- How to change the color of selected text using CSS ?
- How to Create a Dropdown List with Array Values using JavaScript ?
- How to know which radio button is selected using jQuery?
- How to reset selected value to default using jQuery ?
- How to show contents of selected row of a table in Bootstrap model using jQuery ?
- How to get value of selected radio button using JavaScript?
- How to remove the default arrow icon from a dropdown list?
- How to Add Image into Dropdown List for each items ?
- How to get text of specific option tag using jQuery?
- How to get the value in an input text box using jQuery ?
- jQuery | :selected Selector
- How to get multiple selected values of select box in php?
- How to style a dropdown using CSS?
- How to design full width dropdown Navbar using Bootstrap ?
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.

