I created a code with a local storage and so far so good every other thing saves when the page reloads but the text inside the input field vanishes. What should I do please?
I tried adding the local store data to an input field and created and I was expecting it to save the text when the page reloads but it just didn’t save it.
The input is inside a span element but was created with JavaScript
2
Answers
If the text inside your input field is not persisting after a page reload even though you are using local storage, there are a few things you need to check and modify in your code.
At the time when the input field value changes, save it to local storage. You can use the input or change event to trigger the save operation.
And the time the page loads, retrieve the input field value from local storage and set it as the initial value.
Hope this help you 🙂
This example fully works:
As you can see, we can make it work. So something went wrong in the way you tried to make it work. Since the
input
was added via Javascript at your end, there are chances that you attempted to add the event handlers before your input was created. Here I used theoninput
attribute on theinput
which will have the event handler even if theinput
was added via Javascript.