JSFiddle - React, Tailwind, and code Playground

by vi161

HTML

<select id="names">
    <option value="">Please Select</option>
    <option value="ddf">Steve</option>
    <option value="joh">John</option>
    <option value="3">Max</option>
</select>

JavaScript

function indexMatchingText(ele, text) {
    for (var i=0; i<ele.length;i++) {
        if (ele[i].childNodes[0].nodeValue === text){
            return i;
        }
    }
    return undefined;
}

var select =document.getElementById('names').getElementsByTagName("option");

console.log(indexMatchingText(select, "John"));
console.log(indexMatchingText(select, "Steve"));
console.log(indexMatchingText(select, "Max"));
console.log(indexMatchingText(select, "Please Select"));
console.log(indexMatchingText(select, "Foo"));