Going Through Arrays

Once you know how many elements are in an array, you need to write some code that goes through each element. If you have a list of four checkboxes and want to check them all, you could write a script like Figure 8-5.

Checking four checkboxes

Figure 8-5. Checking four checkboxes

The checkFour() function in this script goes through each of the four checkboxes and sets its checked property to true (see the result in Figure 8-6). But this code is not the best solution, since it only works for four checkboxes. To work with five checkboxes, you'd have to add another line to the function.

Figure 8-6. The checkboxes checked

With 1,000 checkboxes the function would ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.