Hello, i have a multiple checkbox that needs checking.
So i build some validation code through javascript.
I found some problem in setting the name of checkbox in a loop.
The loop should check three checkboxes.

Which are status1, status2, and status3.
You might want to see my code below.
It doesnt work.
Anybody please helps.....
Thanks.....

The code:

for(var i=1;i<=3;i++) {
if(document.form1.status+i.checked){
..........................
}else{
..........................
}

Recommended Answers

All 3 Replies

try this...

for(i=1;i<=3;i++) {
tmp = "status" + i;
tmp1 = "document.form1." + tmp ;
alert(tmp1);
if(tmp1.checked){
..........................
}else{
..........................
}

This will work...

Cheers..

try this...

for(i=1;i<=3;i++) {
tmp = "status" + i;
tmp1 = "document.form1." + tmp ;
alert(tmp1);
if(tmp1.checked){
..........................
}else{
..........................
}

This will work...

Cheers..

Hi, thanks for the help.
But your code need some modification to make it work.
here is the modification:


for(i=1;i<=3;i++) {
tmp = "status" + i;
tmp1 = "document.form1." + tmp ;
tmp2 = tmp1+".checked";
if(tmp2){

..........................
}else{
..........................
}

Many thanks for the help azzu....

Another way is shown oin the third post of my thread:

An array of arrays of of radio buttons

I gave all three checkboxes the same name. Then it was simple to do:

for(i=1;i<=3;i++) {
  tmp1 = document.form1.status[i].checked;
  alert(tmp1);
  if(tmp1){
    ..........................
  }
  else{
    ..........................
  };
};

It seems to be the only way to do it with radio buttons.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.