0

I am trying to keep checkboxes checked on refresh with localStorage. All modern browsers do the job except IE11 (and lower)

Fiddle:

http://jsfiddle.net/usme91gk/

(function() {
var boxes = document.querySelectorAll("input[type='checkbox']");
for (var i = 0; i < boxes.length; i++) {
    var box = boxes[i];
    if (box.hasAttribute("store")) {
        setupBox(box);
    }
}

function setupBox(box) {
    var storageId = box.getAttribute("store");
    var oldVal    = localStorage.getItem(storageId);
    console.log(oldVal);
    box.checked = oldVal === "true" ? true : false;

    box.addEventListener("change", function() {
        localStorage.setItem(storageId, this.checked); 
    });
}
})();

Does anyone know if there's a way to somehow adapt this to IE? Or maybe you know a script that keeps checkboxes checked on refresh in all browsers? (Doesn't matter if it's in javascript or jquery). Any help is appreciated.

qwikad
  • 43
  • 9
  • To keep checkbox `checked`, **checked** property will help.. – Rayon Sep 19 '15 at 02:53
  • I am aware of that. How is it relatable to what I am asking though? – qwikad Sep 19 '15 at 03:20
  • LocalStorage should work fine in IE 8+. Check out [this](http://stackoverflow.com/a/23391501/1124565), it may have what you're looking for – amura.cxg Sep 19 '15 at 03:25
  • I just ran your fiddle on Windows 8.1, IE 11.0.9600.17498 and it worked correctly. Maybe you have a version of IE that is out of date or you have security settings that aren't permitting the use of LocalStorage – amura.cxg Sep 19 '15 at 03:29
  • @qwikad, I got it wrong initially. You can use `cookies` over localStorage if lower versions of IF is your concern. – Rayon Sep 19 '15 at 03:30
  • @RayonDabre I searched the web for an hour looking for a good one that uses cookies. Came here to see if maybe someone has one. – qwikad Sep 19 '15 at 03:37
  • By the way is there a way to add a function to the script above that supports cookies? Just to cover some IEs and older FFs. – qwikad Sep 19 '15 at 03:39

1 Answers1

0

I use jstorage when I need localstorage. It has some added features like TTL (time to live).

http://www.jstorage.info

andre mcgruder
  • 1,120
  • 1
  • 9
  • 12