Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you have variables inside your

Status
Not open for further replies.

MartDawg

Programmer
Jan 14, 2004
33
US
Can you have variables inside your javascript objects. For example, would the following code work?

Let's say I have a group of checkboxes named, check1, check2, check3, ect. Up to 20...

Can I do the following...

for(var i=0; i<=20; i++){
if (window.document.form.check&quot;+i+&quot;.checked == true){
Dome some stuff
}
}

Can I put a variable to call an object like that?

I basically need a way to loop through the checkboxes and if they are checked, than I need to validate some text boxes. But I only want to validate the text boxes, if the associated checkbox is checked.
 
yes, but do it this way:

for(var i=0; i<=20; i++){
if (window.document.form[&quot;check&quot; + i].checked == true){
// Do some stuff
}
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
The syntax would be document.formName[&quot;check&quot;+i].checked

See faq216-3858 for more info

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
Dang, Jeff beat me again...I'm still not fast enough [smile]

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
I prefer naming all checkboxes the same and then referencing them like this, but that's just my preference:
Code:
size = formName.checkName.length;
for (i = 0; i < size; i++) {
   if (formName.checkName[i].checked) {
      //blah
   }
}

-kaht

banghead.gif
 
Thanks for the tips guys. Works like a charm!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top