I am new to javaScript. I create a calculator here
I saved the input values ββin variables so that I could eventually manipulate the results to perform input-based calculations. For now, I just want all values ββto be added together.
However, instead of adding, they are concatenated. I used parseInt so that javascript does not look at numbers as strings, and typeOf shows that they are numbers.
Here is my javascript:
$(document).ready(function() { var theTerm = $("#theTerm").val(); var theRate = $("#theRate").val(); var thePrice = $("#thePrice").val(); var theTax = $("#theTax").val(); var theDown = $("#theDown").val(); var theTrade = $("#theTrade").val(); var theResult = parseInt(theTerm + theRate + thePrice + theTax + theDown + theTrade, 10); $("#calculate").click(function(){ alert(theResult); alert(typeof(theResult)); }); });
and HTML:
<div id="calculator"> <span id="calculatorHeader">Monthly Payment Calculator</span> <table style="margin:0 auto;"> <tr> <td style="width:40px;">Term <input id="theTerm" size="5" value="7" name="term" style="width:35px" /> </td> <td style="width:40px;">Rate(%) <input id="theRate" size="5" value="7" name="apr" style="width:35px" /> </td> <td style="width:55px;">Price($) <input id="thePrice" size="6" maxlength="7" name="price" style="width:50px" value="7" /> </td> <td style="width:40px;">Tax(%) <input id="theTax" size="4" maxlength="7" name="tax" style="width:35px" value="7" /> </td> <td style="width:40px;">Down($) <input id="theDown" size="5" maxlength="7" name="downPmt" style="width:35px" value="7" /> </td> <td style="width:40px;">Trade($) <input id="theTrade" size="5" maxlength="7" name="trade" style="width:35px" value="7" /> </td> <td style="width:78px;">Est.Monthly Pmt <input id="theResult" size="7" maxlength="7" name="result" style="width:75px" value="0" /> </td> </tr> </table> <button type="button" id="calculate">Add Boxes!</button> </div>
javascript jquery
HelloWorld
source share