Javascript query...

Status
Not open for further replies.
Looking for a little help with a javascript query...

I have a list of values (in a form field).
I have a list of checkboxes on the screen.

I want to be able to pre-set those checkboxes whose value is contained in the hidden list of values when I press a button.

I can read the list of values from the field and convert to an array using split.

I want to be able to loop over the checkboxes and set them checked if their value is in the array. Can I do this without looping over the values of the array each checkbox iteration?

Straight JS, no frameworks...

Cheers...
 
Is the hidden list of values the check box id or name? Just loop through those and set each to true instead of looping through all of the boxes and checking if its name is in the list.

Code:
var list = "box1,box3,box4,box6".split(",");
for (var i = 0; i < list.length; i++)
     document.getElementById(list[i]).setAttribute("checked","checked");
 
Status
Not open for further replies.