Learn JavaScript – How to Get a Select Value in a Dropdown List?

 
How to recover in JavaScript, the value of an option in a dropdown list? Before anything else, we must remember that we can retrieve different information from that option. Let´s take a look at the terminology used to name different things related to an option.

Consider the example below:


The above option has the following information:

  • Index = 0
  • Value = Hello
  • Text = Hello World

 

JavaScript – How to recover the value of a selected option in a dropdown list

Keeping in mind what we stated above, we know that we can recover the index, the value or text. If you have the example below:


When running the following code:


The return would be the value 2.

ENJOYING THIS POST?

Get More Benefits With LiveEdu Pro

If we want to recover the text of the option selected, the following code can be used:


And the returned value would be test2.
 

JavaScript – Using jQuery

If you can use jQuery, this operation becomes simpler. The code to retrieve the value or text of the selected option can be created following the model below:


These are the ways we had to share this article. Do you know other ways to do this operation in JavaScript?  Share your comments in the section below!

If you want to explore other questions, you can check LCTV videos on JavaScript. Below are some examples:

You can also subscribe to some channels that broadcast in JavaScript, such as the following:

JavaScript How to Get a Select Value from a Dropdown List programmingMichaeljcalkins

JavaScript How to Get a Select Value from a Dropdown List programmingRdegges

Another cool way to find out interesting things about JavaScript is to access our project page!

LiveEdu Pro
Check Out the LiveEdu Pro Subscription

  • Y. de Mello

    Damn! Never thought of using jQuery to do this.

Read previous post:
learn-how-to-code-more-productive-in-less-time
Learn How To Code More Productive in Less Time

Coding and productivity have been the talk of the town for a long time now. There are hundreds of online...

Close