Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the selected item's text in JavaScript?

Tags:

javascript

How can I get the selected item's value and text in JavaScript?

This is my combobox:

<select  size="1" id="fTerminalType" name="fTerminalType">
    <option value="AP">Airport</option>
    <option value="PT">Port</option>
    <option value="BS">Bus</option>
    <option value="TR">Train</option>
</select>

My JavaScript is like this:

var TerminalType = document.getElementById("fTerminalType").value;

Here I can get the value of the combobox. But how can I get the text of the selected value? For example if value was "BS", I need the text "Bus".

like image 230
Alex Avatar asked Sep 22 '09 15:09

Alex


1 Answers

var t = document.getElementById("fTerminalType");
var selectedText = t.options[t.selectedIndex].text;
like image 64
D'Arcy Rittich Avatar answered Nov 15 '22 03:11

D'Arcy Rittich