I created a checkbox list with names. I'm trying to have the person select up to 2 of the names to write to localStorage.
How do I find which checkboxes are checked and write them to localStorage?
Hi Doug,
Run this code:prevar arr = new Array();
Appery("mobilecheckboxgroup").find(":checked").each(function(){
arr.push($(this).val());
});/preWhere 'mobilecheckboxgroup' is a checkbox component name,
'arr' is a name of array with selected values.
That's perfect! What's the easiest way to make sure only two boxes are checked?
Doug,
Do you mean you want to allow 2 boxes only so your user can't choose the 3rd one?
That's correct. Here's what I came up with
precode
var arr = new Array();
Appery("guardianCheckBox").find(":checked").each(function(){
arr.push($(this).val());
});
if(typeof arr[2] !== "undefined")
{
alert("Please select only 2 guardians!");
} else {
if(typeof arr[0] !== "undefined")
{
localStorage.setItem('selectedGuardian',arr[0]);
if(typeof arr[1] !== "undefined")
{
localStorage.setItem('selectedGuardian2',arr[1]);
}
addChild.execute();
}
else
{
alert("Please select a guardian!");
}
}
/code/pre
It checks if there are multiples, or if none have been selected. This way you can have 1 OR 2, but no more or no less.
Look alright?
Ok, so now I'm trying to do the exact opposite: Get the localStorage variables to mark which boxes are checked. Here's my failing code:
precode
var g1 = localStorage.getItem('selectedGuardian');
var g2 = localStorage.getItem('selectedGuardian2');
Appery("guardianCheckBoxList").find("input[value='" + g1 + "']").prop("checked", true);
Appery("guardianCheckBoxList").find("input[value='" + g2 + "']").prop("checked", true);
/code/pre
What else do I need?
Have you tried debugging the code line by line to see where it fails?
Max, I get no errors and nothing out of the ordinary in the debugger.
Here's my updated code:
precode
if (value == localStorage.getItem('selectedGuardian') || value == localStorage.getItem('selectedGuardian2')) {
Appery('guardianListItem',this).prop('checked',true).checkboxradio("refresh");
console.log(value);
}
/code/pre
It consoles the values no problem. I just can't get it to check the boxes!
Did you test that you have the right selector, to select the checkbox? If yes, did you try setting it to static value just for testing?
I wouldn't know how to do this off hand. Sorry...I'm still learning.