There are many many scripts out there that work, but they do not work in all browsers. Does anyone know (or wrote) a script that will keep multiple checkboxes checked even when the page is reloaded that works in ALL modern browsers? I’d prefer something in pure JS rather than jquery.
I know that. I wouldn’t post this question in javascript if I didn’t know how to do this via HTML. I need to keep them checked using either cookies or localStorage. I’d prefer localStorage but it’s nowhere to be found in IE 9+. LOL. So… I am looking to some good old javascript + cookie solution. If possible. Something similar to: http://hibbard.eu/blog/pages/persist-checkboxes/demo-1.html but it doesn’t work in IE.
The following script uses cookies to do what you want. It will only work if cookies are enabled on the client machine and the browser is set to accept cookies. Private browsing may prevent it working. Other than that, the checkboxes are checked correctly on page re-load or on going to another page and returning. The expiry date for each cookie is set for one day. You can change this in the script variable called “tomorrow” if necessary.
The script assumes consistency in the ID of the checkbox <input> elements. For this example I have used CB1 - CB4. The regex looks for CB and one digit. If you want to change the id syntax you will need to change the regex pattern as well.