I have a form with a checkbox that I want to save the status of (true/false) to local storage when the form is submitted.
For example, if a user checks the box and submits the form, the value stored in local storage will be set to "true". However, I don't want this data to be overwritten if the user re-opens the app and submits the form again. If the user decides not to check the box during another session, the local storage values should be "true, false", and so on.
I believe using JSON.stringify would be useful but I'm unsure how to implement it here.
Below is my JavaScript code (using PhoneGap):
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.getElementById("xbutton").addEventListener("click", saveCheck, false);
}
function saveCheck() {
var checkbox = document.getElementById("MedA");
localStorage.setItem("MedA", checkbox.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>