Skip to content
Advertisement

How to capture the text of an Select option?

So I have this:

<select id="list">
    <option value="1">This is Me</option>
    <option value="2">This is You</option>
    <option value="3">And this is Mr. Nukem</option>
</select>

How would I go about grabbing the ‘text’ of the options here? The problem is, it needs to be ‘dynamic’, in the sense I need the text for the currently selected option…

I know a manual, static way of getting the text…

document.getElementById('list').options[1].text

That will grab “This is You”… But how do I get it for the currently selected option? Since I can’t simply use:

document.getElementById('list').value

As that will grab the number… 🙁

Advertisement

Answer

var list = document.getElementById('list');
var text = list.options[list.selectedIndex].text;

See (for example) here https://developer.mozilla.org/en/DOM/HTMLSelectElement

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement