Page 1 of 1

for loop problem

Posted: Sun Sep 23, 2012 10:56 pm
by Lphp
I have for loop inside tee if else

Code: Select all

if((document.fac.type[2].checked==true)||(document.fac.type[3].checked==true) ){
 for (var i=1;i<=6;i++)
                    {
                     document.fac.category[i].disabled=true
                    document.fac.name[i].disabled=true
                    document.fac.post[i].disabled=true
                    }


}
i keep get error Uncaught TypeError: Cannot read property '1' of undefined point to the for loop, what stupid mistake that I made? :(

Re: for loop problem

Posted: Mon Sep 24, 2012 1:13 am
by requinix
document.fac.category, document.fac.name, and/or document.fac.post are undefined. Make sure you're looking at the right objects/arrays in the right places.

Re: for loop problem

Posted: Mon Sep 24, 2012 3:57 am
by Lphp
but the following hard code work fine
document.fac.category1.disabled=false
document.fac.name1.disabled=false
document.fac.post1.disabled=false

Re: for loop problem

Posted: Mon Sep 24, 2012 5:57 am
by requinix
That's great and all but category and category1 are two different things.

Fortunately JavaScript is flexible when it comes to object members.

Code: Select all

document.fac["category" + i]

Re: for loop problem

Posted: Mon Sep 24, 2012 9:12 pm
by Lphp
thanks a lot