Here is what I am trying to do.
First of all, this is in a Chrome extension.
In the file, popup.js, which runs on popup.html (this code is to SET the value, when I choose a select option):
function mySelectValue() {
// Add an event listener for the value
document.getElementById('mySelectValue').addEventListener('change', function() {
// Get the value of the name field.
var mySelectValue = document.getElementById('mySelectValue').value;
// Save the name in localStorage.
localStorage.setItem('mySelectValue', mySelectValue);
});
}
Now after that I have code to retrieve the value that should now be stored in localstorage:
function getAndDisplayTheValue() {
myValue = localStorage.getItem('mySelectValue');
document.write(myValue);
}
Okay so that is the javascript file. Now here is the popup.html file:
<select id=mySelectValue>
<option name= value=>choose a value</option>
<option value=first name=first>first value</option>
<option value=second name=second>second value</option>
<option value=third name=third>third value</option>
</select>
Ok so what I want to do, is:
- When I choose a value, for example
first value
, I want it stored in local storage. So now in localstorage it stored the valuefirst
. - If I decided no i didn't want that and changed to
second value
, then it should overwrite the localstorage and now in localstoragesecond
should be the value that is stored there now. - Next time you open the page (in this case popup.html), instead of the default value being
choose a value
(with empty name and value), instead the default value should be pulled from localstorage so since you chosesecond value
before, nowsecond value
should be the default value.