...

View Full Version : javascript loop question?



angst
09-07-2006, 09:39 PM
Hello,
I'm trying to loop through some javascript, and add a number to the end of the field request, like: document.myForm.myFile" + i,
but i keep getting javascript errors about "unterminated string constant"

my code:



function GetFormInfo(){

var i=0
while (i<=3){

var strFileName = document.myForm.myFile" + i
strFileName = (ParseFileName(strFileName.value));
if(strFileName.length > 0){
document.write(strFileName);
}
i=i+1;

}
}



can some one please show me what i'm doing wrong here?

thanks in advance for your time.
-Ken

A1ien51
09-08-2006, 12:39 AM
because you have a quote sitting there hence why the string is not termenated.

var strFileName = document.myForm.myFile" + i


I really think ou are after

var strFileName = document.myForm.myFile[i];

Eric

angst
09-08-2006, 01:24 AM
hmm, now i'm getting another error:
document.myForm.myFile is null or not an object.

my full code:



<script>

function Left(str, n){
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else
return String(str).substring(0,n);
}

function ParseFileName(FileString){

var strTemp = FileString;
strLeft = (Left(strTemp,strTemp.lastIndexOf("\\") + 1))
myString = strTemp.replace(strLeft,'');
return myString;

}
function GetFormInfo(){

var i=1
while (i<=4){
var strFileName = document.myForm.myFile[i];
if(strFileName.value !== ""){
strFileName = (ParseFileName(strFileName.value));
if(strFileName.length > 0){
document.write(strFileName);
}
}
i=i+1;
}
}
</script>
<form name='myForm'>

<input type='file' name='myFile1'><br />
<input type='file' name='myFile2'><br />
<input type='file' name='myFile3'><br />
<input type='file' name='myFile4'><br />

<input type='text' name='country'>

<input type='button' onclick='GetFormInfo();'>
</form>


thanks again,
-Ken

A1ien51
09-08-2006, 01:47 AM
I am assuming they were named the same....

var strFileName = document.myForm.elements["myFile" + i];

angst
09-08-2006, 01:50 AM
kool, thats better, only thing.
I'm only getting the first field returned,

any ideas?

thanks again! I'm still new to javascript.

cheers,
-Ken

angst
09-08-2006, 03:33 PM
bump!
does any one know why this script only returns the value of the first field?

my full script one more time.




<script>
function Left(str, n){
if (n <= 0)
return "";
else if (n > String(str).length)
return str;
else
return String(str).substring(0,n);
}

function ParseFileName(FileString){

var strTemp = FileString;
strLeft = (Left(strTemp,strTemp.lastIndexOf("\\") + 1))
myString = strTemp.replace(strLeft,'');
return myString;

}
function GetFormInfo(){

var i=1
while (i<=4){
var strFileName = document.myForm.elements["myFile" + i];
if(strFileName.value !== ""){
strFileName = (ParseFileName(strFileName.value));
document.write(strFileName);
}
i=i+1;
}
}
</script>
<form name='myForm'>
<input type='file' name='myFile1'><br />
<input type='file' name='myFile2'><br />
<input type='file' name='myFile3'><br />
<input type='file' name='myFile4'><br />
<input type='text' name='country'>
<input type='button' onclick='GetFormInfo();'>
</form>




thanks again for all your help!
-Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum