JavaScript

How to Get Selected Dropdown Value onchange Event Using jQuery?

In HTML, the β€œ<select>” element creates a dropdown list that acts as a selection list from which the user can select single or multiple options. It is mostly used with HTML forms for the collection of user inputs. By default, it only displays the selected option content on the web browser that is enclosed in opening and closing tags, not its value. The β€œvalue” of the selected option is important because it indicates what is sent to the server when the entered information is submitted.

This post explains all possible approaches for getting jQuery dropdown value onchange event.

How to Get Selected Dropdown Value onchange Event Using jQuery?

jQuery offers a pre-defined β€œval()” method to get a selected dropdown option value upon triggering the onchange event. This method sets and retrieves the β€œvalue” attribute of the selected HTML element. The working of this method depends on its basic syntax which is written below:

$(selector).val()

This section carries out the practical implementation of the β€œval()” method to get the selected dropdown option value when an β€œonchnge” event is fired.

HTML Code

<center>

<p>Select a language from drop-down list</p>

<select id="mySelectbox">

<option value="first">HTML</option>

<option value="second">CSS</option>

<option value="third">JavaScript</option>

<option value="fourth">NodeJS</option>

<option value="fifth">React</option>

</select><br>

</center>

In the above lines of code:

  • The β€œ<center>” tag sets the given content alignment to the center of the web page.
  • The β€œ<p>” tag specifies a paragraph statement.
  • The β€œ<select>” tag adds a select box with the id β€œmySelectbox”.
  • In this β€œselect” element, the β€œ<option>” tag inserts a list of options.

jQuery Code

<script>

$(document).ready(function () {

$("#mySelectbox").on("change", function() {

var value = $(this).val();

alert("Selected option value: " + value)

});

});

</script>

The above jQuery code block:

  • First, apply the β€œready()” method to execute the given function when the current HTML document loads on the web browser.
  • Next, associate the β€œchange” event with the accessed selectbox selector using its id β€œmySelectbox” to execute the specified function when this event is fired.
  • After that, in the function definition, the β€œvalue” variable applies the β€œval()” method to get the selected option β€œvalue” attribute.
  • Lastly, add an alert box to display the β€œvalue” variable output.

Output

It can be seen that the alert box displays the selected option β€œvalue” while triggering the β€œchange” event i.e. changing the option.

How to Get Selected Dropdown Value and Text onchange Event Using jQuery?

Apart from value, the user can also utilize the β€œtext()” method to get the text of the selected option when the onchnage event trigger. Let’s see it practically.

Note: The HTML code is the same as in the β€œval()” method.

jQuery Code

<script>

$(document).ready(function () {

$("#mySelectbox").on("change", function() {

var text = $(this).find("option:selected").text();

var value = $(this).val();

alert("Selected value: " + value + " Selected Text: " + text)

});

});

</script>

The above code block:

  • Declare a variable β€œtext” that uses the β€œfind()” method to first find the selection option via the β€œoption:selected” parameter and then get its text using the β€œtext()” method.
  • The β€œalert box” now displays both the β€œvalue” as well as the β€œtext” variable value.

Output

The above output pops an alert box displaying both the value and text of the selected option from the dropdown value when the β€œonchange” event fires.

Conclusion

To get the selected dropdown option value when an β€œonchange” event fires, use the jQuery β€œval()” method. This method helps to retrieve the β€œvalue” of the selected option if it is specified otherwise it returns its text. Moreover, the user can also retrieve the text of the selected option using the β€œtext()” method. This post explained all the possible approaches for getting selected dropdown value onchange event using jQuery.

About the author

Areej Nadeem

I am a technical author holding a Bachelor’s degree in Computer Science. I am passionate about writing and learning new technologies and sharing my knowledge with the rest of the world.