How to get text value of a selected option in jQuery ?
Introduction: In this article, we will see how we can use jQuery to get the text value from the selected option. While working on some form-related user interface, we often use the select option, and to use that, we will surely need those values that are being selected.
Approach: We will use select option is a very easy method to make a drop-down menu and the val() method that is an inbuilt method in jQuery which is used to returns or set the value of attribute for the selected elements. This method apply on the HTML form elements.
Parameters: This method does not accepts any parameters.
jQuery CDN Link: Add the following code in the head tag of the HTML file.
<script src=”https://code.jquery.com/jquery-3.6.0.min.js” integrity=”sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=” crossorigin=”anonymous”></script>
Example: In this example, we will make a select option element and print values on the browser console that is getting selected by the user using val() method as described above.
Now we have an idea of how to get those values, so we can use those values and use it wherever we want, we are not limited to the console only.
Please Login to comment...