mardi 4 octobre 2016

Duplicate data saved to localstorage

JS:

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
    document.getElementById("xbutton").addEventListener("click",savecheck,false); }

function savecheck(){
        var checkb = document.getElementById("MedA");
        var medAHistory = JSON.parse(localStorage.getItem("MedA") || "[]");
        medAHistory.push(checkb.checked);
        localStorage.setItem("MedA", JSON.stringify(medAHistory));
}

html:

<input type="checkbox" id="MedA" name="Med" value="A">Medication A
 <input id="xbutton" type="button" onClick="savecheck()">

So basically when I submit the form, the status of the checkbox will be saved to localstorage without overwriting. Right now when I click Submit button, duplication of the checkbox status will be saved to localstorage. E.g if I unchecked the box and submit, value in localstorage will be [false, false] even if I only submit once.

Aucun commentaire:

Enregistrer un commentaire