Reputation: 643
For example i want addition value two input 20
& 10
and done output in alert but i get in output value 2010
If the i should have this output 30
, how is fix it?
Demo: http://jsfiddle.net/SHjZM/
<input type"text" name="ok1" value="20">
<input type"text" name="ok2" value="10">
$('button').live('click', function(){
var val_1 = $('input[name="ok1"]').val();
var val_2 = $('input[name="ok2"]').val();
var Sum = val_1 + val_2;
alert(Sum) // in the here output is "2010" i want output "30" as: "20+10=30"
})
Upvotes: 1
Views: 215
Reputation: 2645
you need to convert variable to integers before addition
var Sum = parseInt(val_1, 10) + parseInt(val_2, 10);
Update:
If no radix argument is provided or if it is assigned a value of 0, the function tries to determine the base. If the string starts with a 1-9, it will be parsed as base 10. If the string starts with 0x or 0X it will be parsed as a hexidecimal number. If the string starts with a 0 it will be parsed as an octal number. (Note that just because a number starts with a zero, it does not mean that it is really octal.)
http://devguru.com/Technologies/ecmascript/quickref/parseint.html
Upvotes: 1
Reputation: 943142
You need to convert the strings to (decimal) numbers.
var Sum = parseFloat(val_1, 10) + parseFloat(10, val_2);
Upvotes: 0