Get selected value in dropdown list using JavaScript

How do I get the selected value from a dropdown list using JavaScript?

I tried the methods below, but they all return the selected index instead of the value:

var as = document.form1.ddlViewBy.value;
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

If you have a select element that looks like this:

<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>

Running this code:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

Would make strUser be 2. If what you actually want is test2, then do this:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;

Which would make strUser be test2

Plain JavaScript:

var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;


$("#elementId :selected").text(); // The text content of the selected option
$("#elementId :selected").val(); // The value of the selected option

AngularJS: (

<select ng-model="selectItem" ng-options="item as item.text for item in items">
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>

// JavaScript
$scope.items = [{
  value: 'item_1_id',
  text: 'Item 1'
}, {
  value: 'item_2_id',
  text: 'Item 2'

var strUser = e.options[e.selectedIndex].value;

This is correct and should give you the value. Is it the text you're after?

var strUser = e.options[e.selectedIndex].text;

So you're clear on the terminology:

    <option value="hello">Hello World</option>

This option has:

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

The following code exhibits various examples related to getting/putting of values from input/select fields using JavaScript.

Source Link

Working Javascript & jQuery Demo

enter image description here

enter image description here

 <select id="Ultra" onchange="run()">  <!--Call run() function-->
     <option value="0">Select</option>
     <option value="8">text1</option>
     <option value="5">text2</option>
     <option value="4">text3</option>
<input type="text" id="srt" placeholder="get value on option select"><br>
<input type="text" id="rtt"  placeholder="Write Something !" onkeyup="up()">

The following script is getting the value of the selected option and putting it in text box 1

    function run() {
        document.getElementById("srt").value = document.getElementById("Ultra").value;

The following script is getting a value from a text box 2 and alerting with its value

    function up() {
        //if (document.getElementById("srt").value != "") {
            var dop = document.getElementById("srt").value;

The following script is calling a function from a function

    function up() {
        var dop = document.getElementById("srt").value;
        pop(dop); // Calling function pop

    function pop(val) {

var selectedValue = document.getElementById("ddlViewBy").value;

If you ever run across code written purely for Internet Explorer you might see this:

var e = document.getElementById("ddlViewBy");
var strUser = e.options(e.selectedIndex).value;

Running the above in Firefox et al will give you an 'is not a function' error, because Internet Explorer allows you to get away with using () instead of []:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

The correct way is to use square brackets.

<select id="Ultra" onchange="alert(this.value)"> 
 <option value="0">Select</option>
 <option value="8">text1</option>
 <option value="5">text2</option>
 <option value="4">text3</option>

Any input/form field can use a “this” keyword when you are accessing it from inside the element. This eliminates the need for locating a form in the dom tree and then locating this element inside the form.

Beginners are likely to want to access values from a select with the NAME attribute rather than ID attribute. We know all form elements need names, even before they get ids.

So, I'm adding the getElementByName() solution just for new developers to see too.

NB. names for form elements will need to be unique for your form to be usable once posted, but the DOM can allow a name be shared by more than one element. For that reason consider adding IDs to forms if you can, or be explicit with form element names my_nth_select_named_x and my_nth_text_input_named_y.

Example using getElementByName:

var e = document.getElementByName("my_select_with_name_ddlViewBy");
var strUser = e.options[e.selectedIndex].value;

There are two ways to get this done either using JavaScript or jQuery.


var getValue = document.getElementById('ddlViewBy').selectedOptions[0].value;

alert (getValue); // This will output the value selected.


var ddlViewBy = document.getElementById('ddlViewBy');

var value = ddlViewBy.options[ddlViewBy.selectedIndex].value;

var text = ddlViewBy.options[ddlViewBy.selectedIndex].text;

alert (value); // This will output the value selected

alert (text); // This will output the text of the value selected


$("#ddlViewBy:selected").text(); // Text of the selected value

$("#ddlViewBy").val(); // Outputs the value of the ID in 'ddlViewBy'

Just use

  • $('#SelectBoxId option:selected').text(); for getting the text as listed

  • $('#SelectBoxId').val(); for getting the selected index value

The previous answers still leave room for improvement because of the possibilities, the intuitiveness of the code, and the use of id versus name. One can get a read-out of three data of a selected option -- its index number, its value and its text. This simple, cross-browser code does all three:

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Demo GetSelectOptionData</title>
    <form name="demoForm">
        <select name="demoSelect" onchange="showData()">
            <option value="zilch">Select:</option>
            <option value="A">Option 1</option>
            <option value="B">Option 2</option>
            <option value="C">Option 3</option>

    <p id="firstP">&nbsp;</p>
    <p id="secondP">&nbsp;</p>
    <p id="thirdP">&nbsp;</p>

    function showData() {
        var theSelect = demoForm.demoSelect;
        var firstP = document.getElementById('firstP');
        var secondP = document.getElementById('secondP');
        var thirdP = document.getElementById('thirdP');
        firstP.innerHTML = ('This option\'s index number is: ' + theSelect.selectedIndex + ' (Javascript index numbers start at 0)');
        secondP.innerHTML = ('Its value is: ' + theSelect[theSelect.selectedIndex].value);
        thirdP.innerHTML = ('Its text is: ' + theSelect[theSelect.selectedIndex].text);

Live demo:,output .

id should be used for make-up purposes. For functional form purposes, name is still valid, also in HTML5, and should still be used. Lastly, mind the use of square versus round brackets in certain places. As was explained before, only (older versions of) Internet Explorer will accept round ones in all places.

Using jQuery:


Running example of how it works:

var e = document.getElementById("ddlViewBy");
var val1 = e.options[e.selectedIndex].value;
var txt = e.options[e.selectedIndex].text;

document.write("<br />Selected option Value: "+ val1);
document.write("<br />Selected option Text: "+ txt);
<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2">test2</option>
  <option value="3"  selected="selected">test3</option>

Note: The values don't change as the dropdown is changed, if you require that functionality then an onClick change is to be implemented.

Another solution is:


You can use querySelector.


var myElement = document.getElementById('ddlViewBy');

var myValue = myElement.querySelector('[selected]').value;

I have a bit different view of how to achieve this. I'm usually doing this with the following approach (it is an easier way and works with every browser as far as I know):

<select onChange="functionToCall(this.value);" id="ddlViewBy">
  <option value="value1">Text one</option>
  <option value="value2">Text two</option>
  <option value="value3">Text three</option>
  <option value="valueN">Text N</option>

In 2015, in Firefox, the following also works.


To go along with the previous answers, this is how I do it as a one-liner. This is for getting the actual text of the selected option. There are good examples for getting the index number already. (And for the text, I just wanted to show this way)

let selText = document.getElementById('elementId').options[document.getElementById('elementId').selectedIndex].text

In some rare instances you may need to use parentheses, but this would be very rare.

let selText = (document.getElementById('elementId')).options[(document.getElementById('elementId')).selectedIndex].text;

I doubt this processes any faster than the two line version. I simply like to consolidate my code as much as possible.

Unfortunately this still fetches the element twice, which is not ideal. A method that only grabs the element once would be more useful, but I have not figured that out yet, in regards to doing this with one line of code.

Here is a JavaScript code line:

var x = document.form1.list.value;

Assuming that the dropdown menu named list name="list" and included in a form with name attribute name="form1".

Just do: document.getElementById('idselect').options.selectedIndex

Then you i'll get select index value, starting in 0.

You should be using querySelector to achieve this. This also standardize the way of getting value from form elements.

var dropDownValue = document.querySelector('#ddlViewBy').value;


I don't know if I'm the one that doesn't get the question right, but this just worked for me: Using an onchange() event in your html, eg.

<select id="numberToSelect" onchange="selectNum">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>


function sele(){
    var strUser = numberToSelect.value;

This will give you whatever value is on the select dropdown per click

Here's an easy way to do it in an onchange function:[]



console.log( ddlViewBy.value );

console.log( ddlViewBy.selectedOptions[0].text );
<select id="ddlViewBy">
  <option value="1">Happy</option>
  <option value="2">Tree</option>
  <option value="3"  selected="selected">Friends</option>

The simplest way to do this is:

var value = document.getElementById("selectId").value;

Make a drop-down menu with several options (As many as you want!)

  <option value="giveItAName">Give it a name
  <option value="bananaShark">Ridiculous animal
  <ooption value="Unknown">Give more options!

I made a bit hilarious. Here's the code snippet:

  <option value="RidiculousObject">Banana Shark
  <option value="SuperDuperCoding">select tag and option tag!
  <option value="Unknown">Add more tags to add more options!
<h1>Only 1 option (Useless)</h1>
  <option value="Single">Single Option

yay the snippet worked

In more modern browsers, querySelector allows us to retrieve the selected option in one statement, using the :checked pseudo-class. From the selected option, we can gather whatever information we need:

const opt = document.querySelector('#ddlViewBy option:checked');
// opt is now the selected option, so
console.log(opt.value, 'is the selected value');
console.log(opt.text, "is the selected option's text");
<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>

I think you can attach an event listener to the select tag itself e.g:

          document.addEventListener('DOMContentLoaded', (_) => {.         
          document.querySelector('select').addEventListener('change', e => {

In this scenario, you should make sure you have a value attribute for all of your options