How to get selected text or value of dropdown in Jquery?

Dung Do Tien May 14 2020 129

I have a dropdown as below, it has selected default value, now I want to get both value and text of that selected item.

<select id="myselect">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option selected value="3">Three</option>
  <option value="4">Four</option>
  <option value="5">Five</option>
</select>

It will return value = 3 and text = Three. How can I do it?

Have 1 answer(s) found.
  • Oh, it is easy to do if you are using Jquery.

    Jquery support two methods for you to get text and value option selected, they are text() and val().

    1. To get text using text() method.

    var textSelected = $("#myselect option:selected").text(); // return Three
    

    2. To get value using val() method.

    var valSelected = $("#myselect option:selected").val(); 

    :selected : this is a selector help get the current option selected.

    You can see an example code here.

    Sandeep Kumar May 14 2020
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips