Cycle Through Multiple Checkboxes.

@

@(none)

I have a page which is a set of CheckBoxes generated daily and thus the
number of Checkboxes changes each day.

What I want to do is allow the user to select one or more checkboxes and
the push a "Done" button and then have a script which uses a "for" loop
to check the status of each box. The code I use for this is

Sample checkbox HTML

<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox0"
VALUE="AFL"> &quot;AFL, Score = -2&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox1"
VALUE="ALL"> &quot;ALL, Score = 1&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox2"
VALUE="ANN"> &quot;ANN, Score = 6.15&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox3"
VALUE="BBA"> &quot;BBA, Score = 3&quot;

code to check the State of each checkbox

for ( i=0; i<num; i++ ) {
n = i.toString()
s = "document.Select.CheckBox" +n +".checked"
if ( s ) {
alert ( s )
}
alert ("Done")


If I use "document.Select.CheckBox0.checked" in the "if" it works OK.
The problem is building the "if" parameter in the for loop and maybe I
am missing the point about type conversion - although I have googled for
info and ther doesn't appear to be a type conversion. I dont think I can
use a string in the "if" - so how do I build a type that "if" is happy
with ??

Hope someone can help - Thanks.
 
L

Lee

none said:
I have a page which is a set of CheckBoxes generated daily and thus the
number of Checkboxes changes each day.

What I want to do is allow the user to select one or more checkboxes and
the push a "Done" button and then have a script which uses a "for" loop
to check the status of each box. The code I use for this is

Sample checkbox HTML

<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox0"
VALUE="AFL"> &quot;AFL, Score = -2&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox1"
VALUE="ALL"> &quot;ALL, Score = 1&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox2"
VALUE="ANN"> &quot;ANN, Score = 6.15&quot;
<P STYLE="margin-left: 1.5in"><INPUT TYPE=CHECKBOX NAME="CheckBox3"
VALUE="BBA"> &quot;BBA, Score = 3&quot;

code to check the State of each checkbox

for ( i=0; i<num; i++ ) {
n = i.toString()
s = "document.Select.CheckBox" +n +".checked"
if ( s ) {
alert ( s )
}
alert ("Done")


Use the array notation:

for (var i=0; i<num;i++) {
if(document.Select.elements["CheckBox"+i].checked) {
alert("CheckBox"+i+" is checked");
}
}

or, if there are no other checkboxes in the form, you can just loop through the
form looking at all of the checkboxes, disregarding the names:

for (var i=0;i<document.Select.elements.length;i++) {
var element=document.Select.elements;
if(element.type=="checkbox" && element.checked) {
alert(element.name+" is checked");
}
}

or, if you're not submitting the form to a script that cares about the names,
you could rename all of the checkboxes to "CheckBox", which makes them available
as an array named CheckBox:

for (var i=0;i<document.Select.CheckBox.length;i++) {
if (document.Select.CheckBox.checked) {
alert("CheckBox["+i+"] is checked");
}
}

None of this is tested, beware of typoes.
 
S

sunami

you can put "s" in an eval statement ...

if (eval(s))
....


if you can give each checkbox the same name, you can reference it as an
array and loop over it ...

checkboxes = document.forms[0].nameOfCheckbox;

for (i=0; i < checkboxes.length; i++)
{
if (checkboxes.checked)
{
// do something
}
}
 
M

Mick White

none wrote:
[...]
for ( i=0; i<num; i++ ) {
n = i.toString()
s = "document.Select.CheckBox" +n +".checked"
if ( s ) {
alert ( s )
}
alert ("Done")

for(i=0;i<num;i++){
s=document.Select["CheckBox" +n].checked;
if(s) alert(s);
}
alert ("Done");
Mick
 
M

Michael Winter

Please quote relevant text (delete the rest) from the previous post when
replying.
you can put "s" in an eval statement ...

if (eval(s))
...

The eval function does have it's uses, but this is not one of them.

/* Declare variables that should have local scope
* with the var keyword. In fact, it's good form
* to declare *all* variables with var.
*/
var elements = document.forms['Select'].elements;
for(var i = 0; i < num; ++i) {
if(elements['CheckBox' + i].checked) {
/* ... */
}
}

[snip]

Mike
 
@

@(none)

Mick said:
none wrote:
[...]
for ( i=0; i<num; i++ ) {
n = i.toString()
s = "document.Select.CheckBox" +n +".checked"
if ( s ) {
alert ( s )
}
alert ("Done")

for(i=0;i<num;i++){
s=document.Select["CheckBox" +n].checked;
if(s) alert(s);
}
alert ("Done");
Mick


Hey !! That was really great - the usual story - write 400 lines of code
( some of it really tricky ) and end up getting screwed for a day and a
half trying to test the status of a bloody checkbox.

The line
s=document.Select["CheckBox" +n].checked;

Works perfectly - thanks for all the help ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top