How to get selected text from a drop-down list using jQuery? - GeeksforGeeks

03 Aug, 2021
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="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></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="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></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.
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
jQuery-Misc
Picked
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
jQuery | children() with Examples
How to prevent Body from scrolling when a modal is opened using jQuery ?
How to change the background color of the active nav-item?
jQuery | ajax() Method
How to add `style=display:“block”` to an element using jQuery?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?