for loop problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

for loop problem

Post 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? :(
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: for loop problem

Post 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.
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

Re: for loop problem

Post by Lphp »

but the following hard code work fine
document.fac.category1.disabled=false
document.fac.name1.disabled=false
document.fac.post1.disabled=false
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: for loop problem

Post 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]
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

Re: for loop problem

Post by Lphp »

thanks a lot
Post Reply