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 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    onchange works the first time but if try to change a 2nd field the onchange don't wor

    Here is my code
    <script language="javascript"><!--
    function MilesDriven ()
    {

    alert("in MilesDriven " );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven1"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven1 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven1 is " + MilesDriven1 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven2"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven2 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven2 is " + MilesDriven2 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven3"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven3 =parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven3 is " + MilesDriven3 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven4"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven4 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven4 is " + MilesDriven4 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven5"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven5 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven5 is " + MilesDriven5 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven6"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven6 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven6 is " + MilesDriven6 );

    MilesDriven = parseFloat(worksheet.elements["MilesDriven7"].value);
    if(isNaN(MilesDriven)) { MilesDriven = 0;}
    MilesDriven7 = parseFloat((MilesDriven).toFixed(2));
    alert("MilesDriven7 is " + MilesDriven7 );

    total = MilesDriven1 + MilesDriven2 + MilesDriven3 + MilesDriven4 + MilesDriven5 +MilesDriven6 + MilesDriven7;

    alert("total is " + total );

    document.worksheet.sidetotal2.value = total;


    }
    //--></script>
    <form name="worksheet" action="" method="post" >
    <table align="center" >

    <TR>
    <td> </td>
    <td> Mon 12/16/2013</td> <td> Tue 12/17/2013</td> <td> Wed 12/18/2013</td> <td> Thu 12/19/2013</td> <td> Fri 12/20/2013</td> <td> Sat 12/21/2013</td> <td> Sun 12/22/2013</td> <td >Total</td>
    </tr>
    <tr >
    <td id="TRANSPORTATION 1" nowrap="nowrap" align="right" >TRANSPORTATION </td>
    </tr>
    <tr >
    <td id="MilesDriven 2" nowrap="nowrap" align="right" >Miles Driven </td>
    <td id="MilesDriven1" align="right"> <input name="MilesDriven1" size="10" value="3"
    onchange="MilesDriven(this)" /></td>
    <td id="MilesDriven2" align="right"> <input name="MilesDriven2" size="10" value="35"
    onchange="MilesDriven(this)" /></td>
    <td id="MilesDriven3" align="right"> <input name="MilesDriven3" size="10" value="12"
    onchange="MilesDriven(this)" /></td>
    <td id="MilesDriven4" align="right"> <input name="MilesDriven4" size="10" value=""
    onchange="MilesDriven(this)" /></td>
    <td id="MilesDriven5" align="right"> <input name="MilesDriven5" size="10" value=""
    onchange="MilesDriven(this)" /> </td>
    <td id="MilesDriven6" align="right"> <input name="MilesDriven6" size="10" value=""
    onchange="MilesDriven(this)" /></td>
    <td id="MilesDriven7" align="right"> <input name="MilesDriven7" size="10" value=""
    onchange="MilesDriven(this)" /></td>
    <td> <input name="sidetotal2" readonly="readonly" size="12" value="" /> </td>
    </tr>
    <tr>
    <td align=center colspan="10"><input type="submit" name="SubmitFrm" value="Save" ></td>
    </tr>
    </table >
    </form>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,765
    Thanks
    23
    Thanked 548 Times in 547 Posts
    How many times are you going to ask this?
    Your answer may be on the question you asked the first time
    Go here and look: http://www.codingforums.com/showthre...59#post1381559
    Evolution - The non-random survival of random variants.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Why repeat all that code for each line????

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form id="worksheet">
    <table border="1" padding="3">
    <tr>
        <td>&nbsp;</td>
        <td> Mon 12/16/2013</td> 
        <td> Tue 12/17/2013</td> 
        <td> Wed 12/18/2013</td>  
        <td> Thu 12/19/2013</td> 
        <td> Fri 12/20/2013</td>
        <td> Sat 12/21/2013</td>
        <td> Sun 12/22/2013</td>
        <td >Total</td>
    </tr>
    <tr >
        <td colspan="9">TRANSPORTATION</td>
    </tr>
    <tr>
        <td style="text-align: right;">Miles Driven</td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="MilesDriven" style="text-align: right;"/></td>
        <td><input name="Total" readonly="readonly" style="text-align: right;"/></td>
    </tr>
    </table>
    </form>
    
    <script type="text/javascript">
    (
      function() 
      {
          var form = document.getElementById("worksheet");
          var md = form.MilesDriven;
          for ( var i = 0; i < md.length; ++i )
          {
              md[i].onchange = recalculate;
          }
      
          function recalculate( )
          {
              var sum = 0;
              for ( var i = 0; i < md.length; ++i )
              {
                  var miles = Number( md[i].value );
                  if ( isNaN(miles) )
                  {
                      md[i].value = 0;
                  } else {
                      sum += miles;
                  }
              }
              form.Total.value = sum.toFixed(2);
          }
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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