How to get the HTML select value in jQuery
jQuery is a JavaScript library language that makes DOM manipulation much easier with its features.
Here, we’re going to learn how to get the HTML select value in jQuery.
Get the HTML select value in jQuery
Suppose there’s an HTML select in the DOM; to get its value when selected in jQuery, the .val() method is used.
Note: The
.val()jQuery method returns the value of the element it is invoked on.
An example is given below with a detailed explanation:
Code example
Code explanation
-
Line 1: The document starts with
document.ready(), which makes the jQuery code work. -
Line 2: The
buttonin the HTML file is selected and assigned to it as theclickevent. -
Line 3: This contains the content of the callback function of the
clickevent. This function selects a paragraph element with the idmsgand assigns a text.
The text assigned to it is the value of the select once it’s selected.
$("#ProgrammingLang").val()
The jQuery statement given above is used to get the value of the selected option in an HTML select element with an id of “ProgrammingLang".
Once the button is clicked, the page displays the selected option.
Choose an option and click the “Submit” button to see the magic!
Free Resources