I have several checkboxes with the same name, and values that are a power of 2:
<input type=checkbox name=mycheckbox value=1>Some text 1
<input type=checkbox name=mycheckbox value=2>Some text 2
<input type=checkbox name=mycheckbox value=4>Some text 3
<input type=checkbox name=mycheckbox value=8>Some text 4
<input type=checkbox name=mycheckbox value=16>Some text 5
<input type=checkbox name=mycheckbox value=32>Some text 6
The value stored in the database is the bitwise OR of the selected checkboxes, so if Some text 3 and Some text 6 are checked, the value in the database will be 32 | 4 = 36.
I load this value from the database using an Ajax call. What would be the easiest or best way using jQuery to check the appropriate checkboxes based on the number?
I’m more used to doing scripting and would approach it like:
for (i = 1; i <= 32; i*=2)
if ((ajaxValue & i) == i)
etc.
But ideally jQuery could do it much easier, and check the checkboxes by value.
1 Answer