34
var a = 1;
var b = 2;
var c = a+b;

c will show as 12; but I need 3

How do I do it using jQuery?

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
X10nD
  • 21,638
  • 45
  • 111
  • 152

6 Answers6

84

Definitely use jQuery

Because the power of jQuery is obviously unparalleled, here is how to do with with 100% jQuery:

var a = 1;
var b = 2;
$("<script>c=+("+a+")+ +("+b+")</script>").appendTo($(document));

Now c will hold your result and you used nothing but jQuery! As you can see jQuery is really great because it does all sorts of things

This also works well because it doesn't matter if a or b are strings!


Not enough jQuery?

var a = 1;
var b = 2;
$("<script id='test'>$('<textarea id=\\'abc\\'>'+("+a+")+ +("+b+")+'</textarea>').appendTo($('body'))</script>").appendTo('body');
var c = $("#abc").val();

This answer was done with 100% jQuery because jQuery is awesome but only use this carefully because it might not work sometimes.


jQuery Arithmetic Plugin

You can also use the revolutionary jQuery Arithmetic Plugin this has solved world peace in over 4294967295 (>> 0 === -1) countries:

var a = 1;
var b = 2;
var c = $.add(a,b);

while this is all great (this is not confirmed), but in jQuery -3.0.1 I have heard you will be able to add numbers this way:

$.number($.one, $.two).add($.number($.three, $.four))

this adds 12 (one + two) to 34 (three + four)

Downgoat
  • 13,771
  • 5
  • 46
  • 69
38

It looks like you have strings and not numbers, you need parseInt() or parseFloat() (if they may be decimals) here, like this:

var a = "1";
var b = "2";
var c = parseInt(a, 10) + parseInt(b, 10);
//or: var c = parseFloat(a) + parseFloat(b);

You can test the difference here, it's worth noting these are not jQuery but base JavaScript functions, so this isn't dependent on the jQuery library in any way.

Downgoat
  • 13,771
  • 5
  • 46
  • 69
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
11

Just try this:

var a = 1;
var b = 2;
var c = (+a) + (+b);
alert(c); //or whatever you want
10

Try this -

var c = parseInt(a, 10) + parseInt(b, 10);
Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • 9
    You should always use a radix on `parseInt()`, otherwise numbers starting with a `0` will be treated as base 16 by default. – Nick Craver Sep 28 '10 at 10:12
  • 2
    Watch out with this one, it will use "0" as a magic prefix for octal, so: `parseInt("010") == 8`. – Douglas Sep 28 '10 at 10:14
2

This performs the addition of two variables supplied from input variables selected on the basis of ID.

var salary, tds, netSalary;
salary = parseInt($("#txtSalary").val());
$("#txtTds").on('mouseenter focus', function ()
{
    tds = parseInt($("#txtSalary").val() * (0.1));
    $("#txtTds").val(tds);
});
$("#txtNetSalary").on('mouseenter focus', function () {
    netSalary = parseInt($("#txtSalary").val() +("#txtTds").val());
    $("#txtNetSalary").val(netSalary);
});
1
    var a =10;
    var b=10;
    var c=a+b;
    alert(c);

    //in  double case
     var e=10.5;
     var f=10.5;
     alert(e+f);

     //forcelly convert to int
     alert(parseInt(e)+parseInt(f));

     //forcelly convert to float
     alert(parseFloat(a)+parseFloat(b));


Example   :

 https://jsfiddle.net/v0rjek67/6/