<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<label><input type="checkbox" id="chk1" checked> Show box1</label>
<label><input type="checkbox" id="chk2" checked> Show box2</label>
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<script>
(function() {
var showing;
showing = JSON.parse(localStorage.showing || "null");
if (!showing) {
showing = {
box1: true,
box2: true
};
}
else {
showBox(1, showing.box1);
showBox(2, showing.box2);
}
document.getElementById("chk1").onclick =
document.getElementById("chk2").onclick =
cbclicked;
function cbclicked() {
var num = this.id.replace(/\D/g, '');
var box = "box" + num;
showing[box] = !showing[box];
showBox(num, showing[box]);
localStorage.showing = JSON.stringify(showing);
}
function showBox(num, flag) {
document.getElementById("chk" + num).checked = flag;
document.getElementById("box" + num).style.display =
flag ? "block" : "none";
}
})();
</script>
</body>
</html>