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 5 of 5

Thread: For Loop

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Plano, Texas
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow For Loop

    I have a form with 36 fileds. For demo Fields are named Field1...Field36
    A total Field named SumTotal

    Would someone please show me proper syntax to run a FOR loop to add the fields on a call to a function called Add_Fields() and get the variable sumTotal.

    e.g.: Something like:

    <SCRIPT>
    function Add_Fields(){
    var sumTotal=0
    for(i = ; i <= 36; i++)
    {sumTotal=document.forms.Data_Entry.Field+i.value}
    alert('Your Total'+sumTotal)
    document.forms.Data_Entry.value=sumTotal
    }
    </SCRIPT>

    :o

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <script type="text/javascript">
    function Add_Fields()
    {
    	var sum = 0;
    	for(i = 1; i <= 36; i++)
    	{
    		sumTotal += parseInt(document.forms.Data_Entry.Field+i.value);
    	}
    	alert('Your Total'+sumTotal);
    	document.forms.Data_Entry.total.value = sumTotal;
    }
    </script>

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by hemebond
    Code:
    sumTotal += parseInt(document.forms.Data_Entry.Field+i.value);
    That should be:

    sumTotal += parseInt(document.forms.Data_Entry.elements["Field"+i].value);

    or:

    sumTotal += parseInt(document.forms.Data_Entry["Field"+i].value);


    See the square bracket notation in my sig.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Plano, Texas
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to the forum for fast response....

    hemebond ~ Thank You!

    glenngv ~ Salamat sa inyo



  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by JoeP
    glenngv ~ Salamat sa inyo
    walang anuman.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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