0đź‘Ť
indexOf
returns the -1 when the index is not found so, checking > 0
won’t work if what you’re looking for is the first element in the array. You need >= 0
.
Using the ? true : false
is redundant as that’s what >= 0
(and > 0
) would return anyway.
Also, if kid.id === 1
, this won’t work anyway as 1
is an int and userEditObj.role
is full of strings.
Assuming kid.id
is an int in twig, you probably need to do something like this:
:checked="userEditObj.role.indexOf('{{ kid.id }}') >= 0"
The idea is to output kid.id
as a JS string since the array is filled with string variables.
You may be able to skip the “stringify” step by changing other parts of your code.
let role = ["1", "2", "3"];
let kidId = 1;
let kidIdString = "1";
console.log(role.indexOf(kidId)); // fails. returns -1
console.log(role.indexOf(kidIdString)); // found. returns 0
0đź‘Ť
The problem was with iCkeck lib. Looks like it doesn’t refresh checkboxes that was changed by input attribute. I had to destroy iCkeck and fire it up again. Now works.