jeudi 5 décembre 2019

Save/Looad checkbox state by class name

I am trying to save and load the state of some checkbox:

 function saveCheckbox () {
 var a = document.querySelectorAll('input[type="checkbox"]');
 var array = []; 
 a.forEach(function(checkBox){

array.push({className: checkBox.className, checked: checkBox.checked});
});

 localStorage.setItem('Checkboxes', JSON.stringify(array));
 console.log(JSON.stringify(array)); 
}

Seems to save fine, and checking the log as well, but when I try to load it, it doesnt work at all:

function loadCheckbox(){
var a = JSON.parse(localStorage.getItem('Checkboxes'));

 a.forEach(function(checkBox){

 document.getElementsByClassName(checkBox.className).checked = 
 checkBox.checked;

 });
console.log(JSON.stringify(a)); 
 }

What am I doing wrong here?




Aucun commentaire:

Enregistrer un commentaire