Link to home
Start Free TrialLog in
Avatar of bcrooks
bcrooksFlag for United States of America

asked on

javascript calculation

I'm trying to have javascript calculate an amount when the user inputs a number.

           <script type="text/JavaScript">
                function Calculate()
                 {
                     var TextBox2 = parseInt(document.getElementById('<%= numJazzPresident.ClientID%>').value);
                     var TextBox3 = parseInt(document.getElementById('<%= numWinePaint.ClientID%>').value);
                     var TextBox4 = parseInt(document.getElementById('<%= numVariety.ClientID%>').value);
 
                   if (TextBox2 == " ") {        var jazzDollar = 0;           }
                   else {                       jazzDollar = (parseInt(TextBox2) * 20);            }

                   if (TextBox3 == " ") {         var wineDollar = 0;             }
                   else {                        wineDollar = (parseInt(TextBox3) * 20);           }

                   if (TextBox4 == " ") {           var varietyDollar = 0;              }
                   else {                       varietyDollar = (parseInt(TextBox4) * 10);         }

                   document.getElementById('<%= TotalAmount.ClientID%>').value = alumniDollar + jazzDollar + wineDollar;
    }         </script>


html code

   <asp:TextBox ID="numJazzPresident"  onchange="return Calculate(this)"  runat="server" Width="32px" ></asp:TextBox>
--------------------------------------------
the "totalAmount"   textbox never gets updated
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Russ Suter
Russ Suter

There are a few things wrong with this code. You have undeclared variables and other variables that are declared but go out of scope before they are used in the final line of code.
Do the "empty" text boxes really contain a single space? Otherwise the comparison should be
if (TextBox4 == "") 

Open in new window

Avatar of bcrooks

ASKER

Thanks - its always the simple things
You are welcome