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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variable in for loop

Status
Not open for further replies.

dwessell

Programmer
Jan 26, 2006
114
US
Hey all..

I have a form, and part of that form are some text widgets.. They are all given id's like phoneNumber1, phoneNumber2, etc..Upon page load, they are all given the type of "hidden".

I'm trying to access them with the following for loop:

function teamPopulate(field){
var number = field.value;

for(var i = 1; i >= number; i++){
document.getElementById('phoneNumber' + i).type="text";
}
}

However, I seem to have a syntax error in the 'phoneNumber' + i. If I substitute phoneNumer1 or phoneNumber2 it does work on those options.. So the concept would seem to be sound, at least to my mind. Any suggestions on how to use the 'phoneNumnber' + i?

Thanks
David

 
Are you getting a syntax error, or a runtime error? The code you posted above looks syntactically valid to me, but logically it has a problem: the for loop won't stop until i reaches a value for which you have no 'phoneNumberi defined, which will cause a runtime error (probably "object doesn't have this property or method"). Are you sure you meant "i >= number"?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
actually...

ts got me thinking. you should most definitely be using <= not >=.

the for loop will never work, if you have a number in the "number" variable greater than 1. the middle clause in the for loop (i >= number) is the test statement. the loop will continue until the test statement is no longer met. if you have 3 as the "number" variable, then 1 is less than 3, so the for loop never fires.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Wow.. I love this place.. Two posts that fast.. :)

Clflava.. I have alerted the value of number (First thing I did), and it's exactly what it should be...

tsdragon.. There are no syntax or runtime errors. Either in IE or FF. But the loop control was the problem..

I struggled with this for about 3 hrs last night (First time using JS).. And had made several revisions, including one that went from 9 down, instead of 1 up.. And I had missed that change.. Thanks for calling my attention to the 'stoopid' mistake.

dw

 
We've all made them, and it frequently takes somebody else to point them out to us.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top