I am trying to save an options tag value to local storage. After saving its value to local storage I would like to be able to set the options tag to the option the user selected. I have tried the follow, but I am only able to save the value. I have trouble changing the select tag to the users selected value.
<select name="fruit" id="fruit">
<option value="1">Apple</option>
<option value="2">Guava</option>
<option value="3">Mango</option>
<option value="4">Grapes</option>
</select>
document.getElementById("fruit").onchange = function() {
localStorage.setItem('fruit', document.getElementById("fruit").value);
}
if (localStorage.getItem('fruit') === "Guava") {
document.getElementById("fruit").setAttribute('Selected', 'Guava');
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…