mercredi 28 septembre 2016

Saving multiple checkbox into localstorage without overwritten

I have a checkbox within a form of which I want it to write to localstorage the status of the checkbox (true/false) upon submission of the form.

So if the user checked the box and clicked submit, the value stored in the localstorage will be "true". However what I don't want is for the data to be overwritten if the user re-opens the app and submit the form again, so for example if the user re-opens the app next time and decide to not check the box on that particular session, the values in the localstorage should be "true, false", and so on.

I think I should use JSON.stringify but not sure exactly how to integrate into here.

Here's my js (I'm using phonegap)

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

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

function savecheck(){
        var checkb = document.getElementById("MedA");
        localStorage.setItem("MedA", checkb.checked);
    }

html:

<form>
            <div id='medA'>
                <input type="checkbox" id="MedA" name="Med" value="A">Medication A
             </div>
<input id="xbutton" type="button" onClick="savecheck()" value="Submit">
</form>




Aucun commentaire:

Enregistrer un commentaire