PDA

View Full Version : javascript for loop....



mini_minh
04-13-2007, 12:51 PM
hey was just wondering...if i have like a set of checkboxes or something like...

<input type="checkbox" name="checkbox1"/>
<input type="checkbox" name="checkbox2"/>
<input type="checkbox" name="checkbox3"/>
<input type="checkbox" name="checkbox4"/>
<input type="checkbox" name="checkbox5"/>

how would i write a function to check if the check boxes are checked? like..

function checked()
{
for (i=1; i<= 5; i++)
document.getElementByID("form").....
}

or something like that....
any suggestions?

i know that i can just check each checkbox individually but the checkboxes are created dynamically using an external array and the arrays are of different length so yea...

i need a loop cause theres some values assigned and i need to make a total... like if checkbox is ticked total increases (kind of like a count)

mburt
04-13-2007, 01:09 PM
You're very close, and the idea should work, but it needs a few tweaks.
-Put the inputs in a form
-Give the form the "name" attribute
-Apply it in the script below:

function checked()
{
for (i=1; i<= 5; i++)
if (document.forms["myFormName"].elements["checkbox"+i].checked) {
//The current box in the loop IS checked, do something here.
alert("Checkbox "+i+" is checked.");
}
}

mini_minh
04-13-2007, 01:35 PM
thanx heaps dude
yea i already have the form tags and stuff
just needed the function code cause i wan't sure how to the the loop and yea
thax heaps!!

tech_support
04-14-2007, 05:03 AM
You're very close, and the idea should work, but it needs a few tweaks.
-Put the inputs in a form
-Give the form the "name" attribute
-Apply it in the script below:

function checked()
{
for (i=1; i<= 5; i++)
if (document.forms["myFormName"].elements["checkbox"+i].checked) {
//The current box in the loop IS checked, do something here.
alert("Checkbox "+i+" is checked.");
}
}
Don't you have to declare "i" first?

jscheuer1
04-14-2007, 05:10 AM
Don't you have to declare "i" first?

No, but you should or should declare it formally in the loop. Otherwise, it is part of the global scope and can easily be messed with by other scripts or mess with another global i in another script.

This is good enough:


for (var i=1; i<= 5; i++)

tech_support
04-14-2007, 05:11 AM
That's what I meant :)

Twey
04-14-2007, 11:00 AM
It's kind of inefficient, because you're obtaining a reference to the form again every time the loop iterates. This is one of the most common causes of slowness in scripts. Better would be to get a single reference and store it:
function checked() {
var f = document.forms["myFormName"].elements;
for (var i = 0, f = document.forms['myFormName'].elements, e; e = f[i]; ++i)
if (e.checked) {
// The current box in the loop IS checked, do something here.
alert("Checkbox " + i + " is checked.");
}
}

mburt
04-14-2007, 11:29 AM
Don't you have to declare "i" first?
I was simply adding to the code the OP already had there. I know that you have to use the var keyword.