/    Sign up×
Community /Pin to ProfileBookmark

simple loop wont work–know why?

Hello,

I don’t do much Javascript, so I’m doing something wrong but don’t know what. The loop seems to work fine if I leave out the “[i]” Can anyone see the problem?

var myReps = document.counter.number.value; //No between 1 and 9

if (myReps > 1) {
for (i = 2; i <= myReps; ++i) {
var TempName = formName.TextBox[i].value
document.write(TempName);
}
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanJun 16.2004 — This may be your problem: ++i should be i++ but, try this:

<i>
</i>var myReps = document.counter.number.value; //No between 1 and 9
ver Elms = document.getElementByTagName("textarea");
if (myReps &gt; 1) {
for (i = 2; i &lt;= myReps; ++i) {
var TempName = Elms[i]
var TempNameV = TempName.value;
document.write(TempNameV);
}
}


I'm presuming you arre using a text area for this.
Copy linkTweet thisAlerts:
@garydahlauthorJun 16.2004 — Thanks,

I tried what you suggested (one minor modificzation) below and had the same result.

var myReps = document.counter.number.value; //No between 1 and 9

var Elms = document.formName.textBoxName

if (myReps > 1) {

for (i = 2; i <= myReps; i++) {

var TempName = Elms[i]
var TempNameV = TempName.value;
document.write(TempNameV);

}

}
Copy linkTweet thisAlerts:
@PittimannJun 16.2004 — Hi!

Without referencing your input boxes by tag name and then by type in an extra loop, you do not create an array with their names.

var Elms is referencing one object and Elms[i] would be an array element with the index i.



The other thing is: why are you using document.write()?



Apart from that, maybe this one helps:



var Elms = parseInt(document.formName.textBoxName.value);

if (Elms > 1) {

document.write(Elms );

}



Cheers - Pit
Copy linkTweet thisAlerts:
@garydahlauthorJun 16.2004 — Ah! The problem is that I was not under the impression that I was using an array. I am a novice with Javascript. I thought that by looping through this:

var TempName = document.form.textbox[i].value





you would essentially be doing this:



document.myForm.textbox1.value

document.myForm.textbox2.value

document.myForm.textbox3.value



The .write is just for testing purposes. I'm using the loop to validate a variable number of textboxes. It sounds like I'll have to create an array to iterate through.



Thanks.
Copy linkTweet thisAlerts:
@PittimannJun 16.2004 — Hi!

An example of how to pull them in an array, their values in a second array and then alert the values.[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function getInputs(){
var ElmsOrg=document.getElementsByTagName('input');
var count=0;
var Elms=new Array();
for (var i=0;i<ElmsOrg.length;i++){
if(ElmsOrg[i].type=="text"){
Elms[count] = ElmsOrg[i];
count++;
}
}
for (var i=0;i<Elms.length;i++){
alert(Elms[i].value);
}
}
//-->
</script>
</head>
<body onload="getInputs()">
<form>
<input value="a" name="text1" type="text">
<input value="b" name="text2" type="text">
<input value="c" name="text3" type="text">
</form>
</body>
</html>[/code]
Cheers - Pit
Copy linkTweet thisAlerts:
@garydahlauthorJun 16.2004 — Thanks,

I'm studying your code. one thing I don't understand is this:

var ElmsOrg=document.getElementsByTagName('input');

does it create another array composed of however many inputs you have in the form? and filled with their contents?

And this:

{

Elms[count] = ElmsOrg[i];

count++;

}



it appears to fill the other array with values taken from the textboxes.



Gary
Copy linkTweet thisAlerts:
@PittimannJun 16.2004 — Hi!

var ElmsOrg=document.getElementsByTagName('input');

does it create another array composed of however many inputs you have in the form? - Yes!

and filled with their contents? - No! It "collects" the objects.

And this:

{

Elms[count] = ElmsOrg[i];

count++;

}



it appears to fill the other array with values taken from the textboxes. - No! It pulls all input objects the type of which is "text" (not radio, checkbox etc.) into an array.



This:

for (var i=0;i<Elms.length;i++){

alert(Elms[i].value);

}

references the input type="text" objects and alerts their values.



Cheers - Pit
×

Success!

Help @garydahl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...