samedi 17 décembre 2016

Save checkbox status in one cookie

I have this code:

$("input.box[type=checkbox]").each(function() {
    var name = $(this).attr('name');
    if ($.cookie(name) && $.cookie(name) == "true") {
        $(this).prop('checked', $.cookie(name));
    }
});

$("input.box[type=checkbox]").change(function() {
    var name = $(this).attr("name");
    $.cookie(name, $(this).prop('checked'), {
        path: '/',
        expires: 365
    });
});

and html:

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

<br />

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

<br />

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

etc etc

This makes cookie for every checkbox. What I want to do is save checkboxes status in only one cookie. I'd appreciate your help! Thanks!




Aucun commentaire:

Enregistrer un commentaire