Selected option by text

HTML
<select id="word" name="word"> <option selected="">Please Select</option> <option>aaaa</option> <option>bbbb</option> <option>cccc</option> <option>dddd</option> <option>eeee</option> <option>hello world</option> <option>gggg</option> <option>hhhh</option> <option>iiii</option> <option>jjjj</option> <option>kkkk</option> </select>
CSS
#word { display: block; margin: 4rem; height: 2rem; width: 16rem; }
JAVASCRIPT
'use strict'; function selectCurrentWord(selectObj, textToSet){ for (let i = 0; i < selectObj.options.length; i++) { if (selectObj.options[i].text === textToSet) { selectObj.options[i].selected = true; return; } } } const $select = document.querySelector('select'); selectCurrentWord($select, 'hello world');
Expand for more options Login