How can select specific text value from <select> <option…? [closed]

Okay, from what you have given, I can guess that you need to get the option’s text.

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

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

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

Use this way:

var text = getSelectedText('selectID');

Or as Derek suggested, you can have it in one line too:

var text = elementId.querySelector("option:checked").innerText

Note: This works only in modern browsers.

Leave a Comment