Web Analytics Made Easy -
StatCounter Retrieve number from a form input and return it after rounding - CodingForum

Announcement

Collapse
No announcement yet.

Retrieve number from a form input and return it after rounding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved Retrieve number from a form input and return it after rounding

    I have a order form that adds up all the input boxes (45) and then totals them using function updatePrice() (which works) I’m trying to have the output as XX.XX and can’t figure out how to get the number from the id=”txtTotal” into the function round() and return the formatted number to “txtTotal”.

    thanks for your time
    jim

    Code:
    <script>
    function updatePrice(){ {
    }
    var AZ115R=(document.getElementById('frmOrder').sel0.options[document.getElementById('frmOrder').sel0.selectedIndex].value)*5.40;
    var AZ115R1=(document.getElementById('frmOrder').sel0a.options[document.getElementById('frmOrder').sel0a.selectedIndex].value)*5.15;
    var AZ115R2=(document.getElementById('frmOrder').sel0b.options[document.getElementById('frmOrder').sel0b.selectedIndex].value)*4.90;
    document.getElementById('txtTotal').value=AZ115R + AZ115R1 + AZ115R2;
    }
    </script>
    <head>
    </head>
    <body>
    <form name="form1" action="http://www.bluehost.com/bluemail" enctype="multipart/form-data" method="POST" class="style2" id="frmOrder">  
    <table border="0" width="701">
    	<tr>
    <td width="6">&nbsp;</td>
    <td>
    <img border="0" src="AZ115.JPG" width="129" height="85"></font></td>
    		<td><font color="#CCCCCC"><input type="text" name="AZ115R" value="1AZ115R - Sedona Shrimp" size="35"></font></td>
    		<td>
    <font color="#CCCCCC">
    		<input type="text"  value="$5.40" size="5" name="T2"></font></td>
    		<td><font color="#CCCCCC"><select name="select1" id="sel0" name="sel0">
    <option value="0">00</option>
    <option value="1">01</option>
    <option value="2">02</option>
    <option value="3">03</option>
    <option value="4">04</option>
    <option value="5">05</option>
    </select></font></td>
    		<td><font color="#CCCCCC"><select name="select2" id="sel0a" name="sel0a">
    <option value="0">00</option>
    <option value="6">06</option>
    <option value="7">07</option>
    <option value="8">08</option>
    <option value="9">09</option>
    <option value="10">10</option>
    <option value="11">11</option>
    </select></font></td>
    		<td>
    <font color="#CCCCCC">
    <select name="select3" id="sel0b" name="sel0b">
    <option value="0">00</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    </select></font></td>
    <font color="#CCCCCC"> 
    <input name="data" type="button" value="Click for Total of Order" onclick="updatePrice()">
    $<input  type="text" value="0" name="total order" id="txtTotal" size="8" />
    </font>
    </body>
    Last edited by jim1958; Apr 10, 2009, 12:17 PM.

  • #2
    Code:
    function updatePrice()
    { 
        var form = document.form1;
        var AZ115R  = form.sel0.options[form.sel0.selectedIndex].value*5.40;
        var AZ115R1 = form.sel0a.options[form.sel0a.selectedIndex].value*5.40;
        var AZ115R2 = form.sel0b.options[form.sel0b.selectedIndex].value*5.40;
        document.getElementById("txtTotal").value 
            = Math.round( 100 * AZ115R + AZ115R1 + AZ115R2 ) / 100 ).toFixed(2);
    }
    A strange looking form, I must say.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      Originally posted by Old Pedant View Post
      Code:
      function updatePrice()
      { 
          var form = document.form1;
          var AZ115R  = form.sel0.options[form.sel0.selectedIndex].value*5.40;
          var AZ115R1 = form.sel0a.options[form.sel0a.selectedIndex].value*5.40;
          var AZ115R2 = form.sel0b.options[form.sel0b.selectedIndex].value*5.40;
          document.getElementById("txtTotal").value 
              = Math.round( 100 * AZ115R + AZ115R1 + AZ115R2 ) / 100 ).toFixed(2);
      }
      A strange looking form, I must say.
      Thanks for your reply. I try the code, but it’s given an error (Expected ‘;’ ) If I remove / 100) it works, of course the output number needs to be /by 100. It looks like it should work. I never thought about doing it that way.
      Again thanks for your help.
      jim

      PS: The code that I posted was just a small sample of the page.

      Comment


      • #4
        Missing bracket.

        Code:
        document.getElementById("txtTotal").value = Math.round[COLOR="Red"]([/COLOR]( 100 * AZ115R + AZ115R1 + AZ115R2 ) / 100 ).toFixed(2);
        "We dispense with accuracy" - sign in Chemist's shop.

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          Thanks for your help! (and Phillip M) I did get it working, but found out I didn’t need the Math.round, so I remove it and just left the .toFix and it works great. Now that my problem is resolved I see that you also included a lesson in coding my function updatePrice()… much simpler.
          thanks

          Comment

          Working...
          X