Page 1 of 3

Multiple Checkbox Values to localStorage

Posted: Fri Jan 17, 2014 4:22 pm
by Doug Black

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.

Image

How do I find which checkboxes are checked and write them to localStorage?


Multiple Checkbox Values to localStorage

Posted: Fri Jan 17, 2014 4:51 pm
by Kateryna Grynko

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.


Multiple Checkbox Values to localStorage

Posted: Fri Jan 17, 2014 7:13 pm
by Doug Black

That's perfect! What's the easiest way to make sure only two boxes are checked?


Multiple Checkbox Values to localStorage

Posted: Fri Jan 17, 2014 8:19 pm
by Kateryna Grynko

Doug,

Do you mean you want to allow 2 boxes only so your user can't choose the 3rd one?


Multiple Checkbox Values to localStorage

Posted: Fri Jan 17, 2014 11:47 pm
by Doug Black

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?


Multiple Checkbox Values to localStorage

Posted: Mon Jan 20, 2014 7:50 pm
by Doug Black

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?


Multiple Checkbox Values to localStorage

Posted: Mon Jan 20, 2014 9:21 pm
by maxkatz

Have you tried debugging the code line by line to see where it fails?


Multiple Checkbox Values to localStorage

Posted: Tue Jan 21, 2014 12:03 am
by Doug Black

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!


Multiple Checkbox Values to localStorage

Posted: Tue Jan 21, 2014 1:15 am
by maxkatz

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?


Multiple Checkbox Values to localStorage

Posted: Tue Jan 21, 2014 1:28 am
by Doug Black

I wouldn't know how to do this off hand. Sorry...I'm still learning.