Retrieving the text of the selected <option> in <select> element

JavascriptHtmlDom

Javascript Problem Overview


In the following:

<select id="test">
    <option value="1">Test One</option>
    <option value="2">Test Two</option>
</select>

How can I get the text of the selected option (i.e. "Test One" or "Test Two") using JavaScript

document.getElementsById('test').selectedValue returns 1 or 2, what property returns the text of the selected option?

Javascript Solutions


Solution 1 - Javascript

function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}

var text = getSelectedText('test');

Solution 2 - Javascript

If you use jQuery then you can write the following code:

$("#selectId option:selected").html();

Solution 3 - Javascript

document.getElementById('test').options[document.getElementById('test').selectedIndex].text;

Solution 4 - Javascript

Under HTML5 you are be able to do this:

document.getElementById('test').selectedOptions[0].text

MDN's documentation at https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions indicates full cross-browser support (as of at least December 2017), including Chrome, Firefox, Edge and mobile browsers, but excluding Internet Explorer.

Solution 5 - Javascript

selectElement.options[selectElement.selectedIndex].text;

References:

Solution 6 - Javascript

The options property contains all the <options> - from there you can look at .text

document.getElementById('test').options[0].text == 'Text One'

Solution 7 - Javascript

You can use selectedIndex to retrieve the current selected option:

el = document.getElementById('elemId')
selectedText = el.options[el.selectedIndex].text

Solution 8 - Javascript

this.options[this.selectedIndex].innerText

Solution 9 - Javascript

If you found this thread and wanted to know how to get the selected option text via event here is sample code:

alert(event.target.options[event.target.selectedIndex].text);

Solution 10 - Javascript

Use the select list object, to identify its own selected options index. From there - grab the inner HTML of that index. And now you have the text string of that option.

<select onchange="alert(this.options[this.selectedIndex].innerHTML);">
       <option value="">Select Actions</option>
       <option value="1">Print PDF</option>
       <option value="2">Send Message</option>
       <option value="3">Request Review</option>
       <option value="4">Other Possible Actions</option>
    </select>

Solution 11 - Javascript

The :checked selector can be used with document.querySelector to retrieve the selected option.

let selectedText = document.querySelector('#selectId option:checked').text;
// or
let selectedText = document.querySelector('#selectId')
      .querySelector('option:checked').text;

document.querySelector('button').addEventListener('click', function(e) {
  console.log(document.querySelector('#selectId option:checked').text);
});

<select id="selectId">
  <option>a</option>
  <option>b</option>
  <option>c</option>
</select>
<button>
Get selected text
</button>

For select elements with the multiple attribute, document.querySelectorAll can be used to obtain all selected options.

let selectedText = [...document.querySelectorAll('#selectId option:checked')]
   .map(o => o.text);

document.querySelector('button').addEventListener('click', function(e) {
  let selectedText = [...document.querySelectorAll('#selectId option:checked')]
                      .map(o => o.text);
  console.log(selectedText);
});

<select id="selectId" multiple>
  <option>a</option>
  <option>b</option>
  <option>c</option>
</select>
<button>
Get selected text
</button>

Solution 12 - Javascript

Similar to @artur just without jQuery, with plain javascript:

// Using @Sean-bright's "elt" variable

var selection=elt.options[elt.selectedIndex].innerHTML;

Solution 13 - Javascript

Easy, simple way:

const select = document.getElementById('selectID');
const selectedOption = [...select.options].find(option => option.selected).text;

Solution 14 - Javascript

It is pretty simple. In javascript anything with an ID doesn't need document.queryselector or $('#test') you can just use test. Then you simply loop over the selectedOptions which is apart of javascript and you can add it to a new array and use that data how ever you want.

let selectedItems = [];
for ( var i = 0; i < test.selectedOptions.length; i++) {
    selectedItems.push(test.selectedOptions[i].text);
}

Also

// if you want values
selectedItems.push(test.selectedOptions[i].value);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCountZeroView Question on Stackoverflow
Solution 1 - JavascriptSean BrightView Answer on Stackoverflow
Solution 2 - JavascriptarturgrigorView Answer on Stackoverflow
Solution 3 - JavascriptwormhitView Answer on Stackoverflow
Solution 4 - JavascriptdavidjbView Answer on Stackoverflow
Solution 5 - Javascriptuser669677View Answer on Stackoverflow
Solution 6 - JavascriptGregView Answer on Stackoverflow
Solution 7 - JavascriptjamshidView Answer on Stackoverflow
Solution 8 - JavascriptPhani CRView Answer on Stackoverflow
Solution 9 - Javascriptzeros-and-onesView Answer on Stackoverflow
Solution 10 - Javascriptuser6749930View Answer on Stackoverflow
Solution 11 - JavascriptUnmitigatedView Answer on Stackoverflow
Solution 12 - JavascriptviditkothariView Answer on Stackoverflow
Solution 13 - JavascriptPaweł BednarczykView Answer on Stackoverflow
Solution 14 - JavascriptGoddardView Answer on Stackoverflow