View Full Version : Adding Values in input tag

Steve Sea
11-03-2007, 02:45 PM
Hi Chaps
Got a problem, I'm using a small script when you select a button with a number on it, it returns that number in written word,

ie. press button 10 and the word "Ten" pops up this is all working OK and the back bone of the code is below what I'm trying to re produce now is if someone presses "100 + 30" it will return (in figures) "130" so is there anyway of adding up the values in the Input tag and displaying it. if you need to see why you can pop to my website, its to place the input in the amount box so visitors can double check the amounts they have entered


<input name="Thousands" type="button" onclick="showNumber1('One thousand');" value="1000"><br>

Any help or comments are appreciated

Steve C.

11-03-2007, 03:13 PM
Well, your script is obfuscated, so I'm not going to try to give you any details as to how to edit it. However, I can say that when adding values (strings really) like:


you will get:


If this is the problem, you need to make them into numbers first, example:

var n1="100";
var n2="30";

Now you get:


There are less processor intensive ways to turn a string of this type (one that contains only numeric characters) into a number in javascript, you could do:

var n1="100";
var n2="30";

and still get:


I mention this only if it is workable in your code. If it is, it is better, if not, use the parseInt() method.

Also, if you are dealing with decimal points, you will need to use the parseFloat() method, or figure out a way to first get the decimal point out, do the math, and then put the decimal point back in the appropriate spot.

It is always best to avoid parseFloat if at all possible. It's results can sometimes be off by a minuscule amount. When that happens, you get a repeating decimal value like:


instead of:


Using parseFloat() is also the most resource intensive method of the three I've mentioned.

Steve Sea
11-03-2007, 04:57 PM
I understand my code is hard to follow I have encrypted the javascript to TRY and stop my code getting ripped, so sorry for that.
I think I will play with the javascript example, and see if I get any results and if it make the script that much heaver.

Many Thank

Steve C.