mardi 20 décembre 2016

Saving multiple checkboxes values into localStorage

The problem that I'm having is that the checkbox value doesn't change. What am i missing? Help appreciated!

html:

<input type="checkbox" name="smth1" />something1<br />
<input type="checkbox" name="smth2" />something2<br />
<input type="checkbox" name="smth3" />something3<br />
<br />

<input type="checkbox" name="anthr1" />anotherthing1<br />
<input type="checkbox" name="anthr2" />anotherthing2<br />
<input type="checkbox" name="anthr3" />anotherthing3<br />
<br />

<input type="checkbox" name="new1" />newthing1<br />
<input type="checkbox" name="new2" />newthing2<br />
<input type="checkbox" name="new3" />newthing3<br />
etc etc

script:

$("input[type=checkbox]").each(function() {
    var name = $(this).attr('name');

     if (localStorage.getItem(name) == "on") {
        $(this).prop('checked', true);
    }     
});

$("input[type=checkbox]").change(function() {

    var name = $(this).attr('name'),
    value = $(this).val();

    localStorage.setItem(name,value);                  
});




Aucun commentaire:

Enregistrer un commentaire