javascript - select <select> item by value -
i have
<select id="x"> <option value="5">hi</option> <option value="7">hi 2</option> </select>
i want javascript function enables me select , display <option>
default 1 id. in other words, want setoption(5)
, <option>
value "5" displayed in combobox default .
is possible?
if can, es6...
function setoption(selectelement, value) { return [...selectelement.options].some((option, index) => { if (option.value == value) { selectelement.selectedindex = index; return true; } }); }
...otherwise...
function setoption(selectelement, value) { var options = selectelement.options; (var = 0, optionslength = options.length; < optionslength; i++) { if (options[i].value == value) { selectelement.selectedindex = i; return true; } } return false; } setoption(document.getelementbyid('my-select'), 'b');
if returns false
, value not found :)
Comments
Post a Comment