JavaScript select option using text without jQuery

To select an option by searching for the option's text value without jQuery, use the filter method.

    <option>Select an option...</option>
    <option value="1">Alpha</option>
    <option value="2">Bravo</option>
    <option value="3">Charlie</option>

Select all the options using document.querySelectorAll. Then filter by matching the innerText.

var bravoChoice = Array.from(document.querySelectorAll('option'))
    .filter(option => option.innerText === 'Bravo')[0];
bravoChoice.selected = true;
View this page on GitHub.
Posted .


Leave a Reply