Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Multiple Checkbox Values 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.

Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Multiple Checkbox Values to localStorage

Doug,

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

Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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?

Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Multiple Checkbox Values to localStorage

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

Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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!

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Multiple Checkbox Values to localStorage

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?

Doug Black
Posts: 0
Joined: Wed Aug 14, 2013 11:36 am

Multiple Checkbox Values to localStorage

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

Return to “Issues”