PDA

View Full Version : Any (simple) way with JS to use localstorage to remember entries in all text boxes?



Neil1
04-26-2017, 07:14 PM
Hi, I am working on a quiz page which will have lots (probably 1000) text boxes to enter answers to, ideally I like to make it possible for user to refresh or leave page and return without losing progress... I have worked out by googling some examples how to do it with localstorage, but only with unique IDs for each text box... Is there anyway of targeting all text boxes? The examples I've found seem to reference each individually but uniquely listing 1000 different ones is a pain in the ass if there is another way.

Many thanks to anyone who may be able to help

molendijk
04-26-2017, 08:22 PM
Localstorage should be possible with document.querySelectorAll (instead of document.getElementById). Have you tried that?

jscheuer1
04-26-2017, 09:12 PM
One thing to bear in mind with localStorage is that, if it's not enabled in the user's browser, it can cause a fatal error when code goes to use it. Because of that I tend to rely upon cookies for this sort of thing. I've read that a try/catch can be used to avoid the error inherent in (at least Chrome style) browsers, but I've yet to test that theory and do know that Chrome barfs (gives a fatal error) on localStorage if it's not enabled and not in a try/catch. That doesn't mean try/catch will work for avoiding that though.