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 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calcultate Amount Total according to dropdown

    I have a text field.
    I want the text field to change / be calculated on the action made on the dropdown menu...

    So if I select Debit card.. it should leave the amount as is..
    If I selected Credit Card it should add a percentage to the Amount.

    Please dont just provide a solutoin.. but please explain as i would love to understand why

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
         
    	   function  doMath() { 
    	   
    	   if (CardType = "cc") { 
           var one = eval(document.myForm.Amount.value)
           var prod = one  *   1.03
           document.myForm.Amount.value=custRound(prod,2);}
          }
    	  
    	  else
    	  document.myForm.Amount.value;
         
         function custRound(x,places) {
          return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
         }
        </script>
    
    
    </head>
    
    <body>
    <form id="myForm" name="myForm" method="post" action="">
      <input name="Amount" type="text" id="Amount" v alue="100" />
      <select id="CardType" name="CardType" onchange="doMath();" >
        <option value="" selected="selected">Please Select</option>
        <option value="dc">Debit Card</option>
        <option value="cc">Credit Card</option>
    
      </select>
    </form>
    </body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    You have several syntax errors. If you use FF, look at the 'error console' contents in the "Tools" menu
    For example: Select box value="100" not v alue="100"
    and '=' is an assignment command while '==' is a comparison command for the "if" statement

    I put several comments to other fixes into your code.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
         
    function  doMath(info) { 
      total = document.myForm.Amount.value;
      if (info == "cc") { // comparison is '==' NOT '=' (that is assignment)
        var prod = total * 1.03;
        document.myForm.Amount.value=custRound(prod); 
      }
    }  
    function custRound(x) {   
    // function custRound(x,places) {
    // awful lot of work to round a number
    //   return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
    // when you can simply call the toFixed() function as in
      return x.toFixed(2);
    }
    </script>
    
    
    </head>
    
    <body>
    <form id="myForm" name="myForm" method="post" action="">
      <input name="Amount" type="text" id="Amount" value="100" />
      <select id="CardType" name="CardType" onchange="doMath(this.value)" >
        <option value="" selected="selected">Please Select</option>
        <option value="dc">Debit Card</option>
        <option value="cc">Credit Card</option>
      </select>
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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