I would like to load a selection window in which the value selected by the user will be automatically displayed.
I get Json data from a server with user information. sample data:{"color":"red"}
In my html code, I have a selection option:
<select id="input_user" class="selectinput" disabled="true">
<option value="n/a"> n/a </option>
<option value="red"> red </option>
<option value="green"> green </option>
<option value="yellow"> yellow </option>
<option value="blue"> blue </option>
<option value="white"> white </option>
</select> //this will only show "n/a" as default
I tried this code to make the red default, but it doesn’t work.
var user_color= data[2].color;
document.getElementById('input_user').selectedIndex = user_color;
Any help?
source
share