21

I have code :

function compute() {
    if ($('input[name=type]:checked').val() != undefined) {
        var a = $('input[name=service_price]').val();
        var b = $('input[name=modem_price]').val();
        var total = a + b;
        $('#total_price').val(a + b);
    }
}

In my code I want sum values of two text inputs and write in a text input that has an id of "total"

My two numbers don't sum together for example :

service_price value = 2000 and modem_price=4000 in this example total input value must be 6000 but it is 20004000

2
  • meta.stackexchange.com/questions/45176/… Commented Jul 1, 2011 at 20:39
  • I am thinking seriously of writing a jQuery plugin to do the parseInt with radix solution. Commented Jan 29, 2013 at 23:32

7 Answers 7

27

Your code is correct, except you are adding (concatenating) strings, not adding integers. Just change your code into:

function compute() {
    if ( $('input[name=type]:checked').val() != undefined ) {
        var a = parseInt($('input[name=service_price]').val());
        var b = parseInt($('input[name=modem_price]').val());
        var total = a+b;
        $('#total_price').val(a+b);
    }
}

and this should work.

Here is some working example that updates the sum when the value when checkbox is checked (and if this is checked, the value is also updated when one of the fields is changed): jsfiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

If you are using parseInt like this you will almost certainly want to specify the radix: parseint(userdata, 10); Without this a zero-padded number such as e.g. 08 will be interpreted as octal, resulting in surprising values.
26

Because at least one value is a string the + operator is being interpreted as a string concatenation operator. The simplest fix for this is to indicate that you intend for the values to be interpreted as numbers.

var total = +a + +b;

and

$('#total_price').val(+a + +b);

Or, better, just pull them out as numbers to begin with:

var a = +$('input[name=service_price]').val();
var b = +$('input[name=modem_price]').val();
var total = a+b;
$('#total_price').val(a+b);

See Mozilla's Unary + documentation.

Note that this is only a good idea if you know the value is going to be a number anyway. If this is user input you must be more careful and probably want to use parseInt and other validation as other answers suggest.

1 Comment

A correct explanation and a good example, thank you @Sorpigal
7

use parseInt as a = parseInt($('input[name=service_price]').val())

4 Comments

@ebad gh: .val() returns a string, so your attempted math operation just concatenates the strings. You need to convert the values to integers first, as Rodolfo suggested.
For validity's sake, use parseInt($('input[name=service_price]').val(), 10) instead. You need a radix.
@teddyrised: I've never used a radix because 10 is always supplied as the default. Why is it required?
@George Cummins: Try parseInt("080").
5

use parseInt

   var total = parseInt(a) + parseInt(b);


    $('#total_price').val(total);

1 Comment

Thanks for being the only answer that caught that OP made a var total and never used it :P
2

<script>
$(document).ready(function(){
var a =parseInt($("#a").val());
var b =parseInt($("#b").val());
$("#submit").on("click",function(){
var sum = a + b;
alert(sum);
});
});
</script>

1 Comment

Can you comment your answer?
1

Cast them to a Number

$('#total_price').val(Number(a)+Number(b));

But before you do that

if (!isNaN($('input[name=service_price]').val()) {...

Comments

-1

if in multiple class you want to change additional operation in perticular class that show in below example

$('.like').click(function(){    
var like= $(this).text();
$(this).text(+like + +1);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.