vendredi 3 février 2017

.prop("checked", false) makes checkbox true

I have some <input value=name type=checkbox> whose values must be persistent. So I stored in Cookies the value of every of them, and I wrote a function which must restore the values when the page is ready.

The function which save the cookies is:

function setAllCookies() {
    var checkboxs = $("#divwheretheinputsare input");
    for (i in checkboxs)
        Cookies.set(checkboxs[i].value,checkboxs[i].checked);
}

And the function to restore them is:

function readAllCookies() {
    var checkboxs = $("#divwheretheinputsare input");
    for (i in checkboxs) {
        var nom = checkboxs[i].value;
        var value = Cookies.get(nom);
        $("##divwheretheinputsare input[value='" + nom + "']").prop("checked", value);
    //Also tried $("##divwheretheinputsare input[value='" + nom + "']")[0].checked = value;
    }
}

I can confirm the Cookies are stored with the correct name and the two functions are completly executed. With debugging, I detect this: When the line $("##divwheretheinputsare input[value='" + nom + "']").prop("checked", value) is executed, the checkbox always turn checked, no matter if value is false. If I execute that exact line in console, the checkbox change correctly...

Also, the alternative codeline, is correctly executed in the console but in the function it drops: "TypeError: $(...)[0] is undefined".




Aucun commentaire:

Enregistrer un commentaire