CountZero CountZero - 1 year ago 61
HTML Question

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

In the following:

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

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

returns 1 or 2, what property returns the text of the selected option?

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

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

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

var text = getSelectedText('test');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download