Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    javascript loop question?

    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:
    PHP 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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, now i'm getting another error:
    document.myForm.myFile is null or not an object.

    my full code:

    PHP Code:
    <script>

    function 
    Left(strn){
        if (
    <= 0)
            return 
    "";
        else if (
    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

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I am assuming they were named the same....

    var strFileName = document.myForm.elements["myFile" + i];
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    bump!
    does any one know why this script only returns the value of the first field?

    my full script one more time.

    PHP Code:

    <script>
    function 
    Left(strn){
        if (
    <= 0)
            return 
    "";
        else if (
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •