very simple form onchange - calculate qty*price, display price

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scentedstars
    New Member
    • Mar 2010
    • 2

    very simple form onchange - calculate qty*price, display price

    I had this funtion working by using the onsumbit, but now I have to change it so that it will display the total price after changing the quanity amount.

    Simple I know, but right now i can not get it to work.

    thankful for any help.

    cheers.

    Code:
    <script language="javascript" type="text/javascript">
    
        function checkQuantity(frm) {
            var succesful = false;
            var numQuantity;
            numQuantity = parseInt(frm.Quantity.value);
            var numTotalPrice;
    
            if (numQuantity != 0) {
                numTotalPrice = numQuantity * 4;
                frm.TotalPrice.value = numTotalPrice; //new String(numTotalPrice);
                successful = true;
            }
            else {
                alert("Sorry must order more than 0");
                successful = false;
            }
            return succesful;
        }
                
    
    </script>
    
    
    <form action=""  onsubmit="return checkQuantity(this)" style="width: 193px">
                       
                        <table cellpadding="4" style="width:111%">
                            <tr>
                                <td class="style1">
                                    Quanity:</td>
                                <td class="formInputCell">
                                    <input name="Quantity" type="text" style="width: 55px" /></td>
                            </tr>
                            <tr>
                                <td class="style1">
                                    Total Price $
                                </td>
                                <td class="formInputCell">
                                    <input name="TotalPrice" type="text" style="width: 55px" /></td>
                            </tr>
                            <tr>
                            <td class="style2"></td>
                            <td><input id="Submit1" type="submit" 
                            value="Buy Now" class="buttonstyle" /></td>
                            </tr>  
                        </table>    
                        </form>
    Last edited by gits; Mar 21 '10, 12:12 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    just move the function call to a onchange handler of the corresponding input-node and pass the reference to the form to it ... you might even retrieve the form within the function ... as you like :)

    kind regards

    Comment

    • scentedstars
      New Member
      • Mar 2010
      • 2

      #3
      thanks so much I followed your advice.

      Comment

      Working...