Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Checkbox tile state question

ANSWERED

I have a TileList with checkbox-tile's and using code similar to the code below to determine if a box is selected or not. I can reference each checkbox by tileState[0], tileState[1], tileState[n], so it seems to mean the tileState is an array. However, if I try tileState.length, I get an error. So, I'm not sure if tileState is an array or not. Is there a way to loop through the tileState like you would an array?

 

let tileState = {}

tileItems.forEach((element, index) => {
  tileState[index] = element.firstChild.value; // initial state
  element.firstChild.onclick = (evt) => {
    tileState[index] = !tileState[index];
    console.log(`item ${index} :: ${tileState[index] ? "checked" : "unchecked"}`)
  };
});
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I declared the tileState as an array and that fixed the issue. I took the code from the manual and didn't know if there was a reason why the manual declares the tileState as an object.

View best answer in original post

Best Answer
0 Votes
3 REPLIES 3

Can you provide a bit more code? You've declared tileState as an object in the example. Thanks

Best Answer
0 Votes

This code came from the  View Components Guide. I noticed tileState was declared as an object and not an array. I wasn't sure if there was a reason for doing it that way. 

 

let tileState = {}

tileItems.forEach((element, index) => {
  tileState[index] = element.firstChild.value; // initial state
  element.firstChild.onclick = (evt) => {
    tileState[index] = !tileState[index];
    console.log(`item ${index} :: ${tileState[index] ? "checked" : "unchecked"}`)
  };
});
Best Answer
0 Votes

I declared the tileState as an array and that fixed the issue. I took the code from the manual and didn't know if there was a reason why the manual declares the tileState as an object.

Best Answer
0 Votes