-3

I have this javascript code when i use this function on a button it just sticks the two input texts but i want to add them for example: the result is : 2+3=23 but i want 2+3=5 where is my problem?

<script type="text/javascript">
    function Add()
    {
        var a;
        var b;
        var c;
        a = document.getElementById('Text1').value;
        b = document.getElementById('Text2').value;
        c = a - b;
        document.getElementById('Text3').value = c;
    }
</script>
8
  • 4
    + is concatenation for strings.
    – raina77ow
    Dec 17, 2013 at 14:00
  • so how can i add two numbers? Dec 17, 2013 at 14:00
  • You need to make numbers out of them rather than a String.
    – putvande
    Dec 17, 2013 at 14:00
  • use parseInt(number1) + parseInt(number2)
    – adi rohan
    Dec 17, 2013 at 14:01
  • 1
    Turn strings into numbers (the easiest way to do this with unary plus).
    – raina77ow
    Dec 17, 2013 at 14:01

6 Answers 6

3

The issue is that document.getElementById('Text1').value returns string value, so adding one string to another does string concatenation.

You should cast your values to numbers with parseInt() or parseFloat(), e.g.:

a = document.getElementById('Text1').value;  // "2"
b = document.getElementById('Text2').value;  // "3"
c = parseFloat(a) + parseFloat(b);           //  5

USEFUL: https://softwareengineering.stackexchange.com/questions/90203/why-is-so-bad-for-concatenation

2

Using the infix operator is shorter:

a = +document.getElementById('Text1').value;
b = +document.getElementById('Text2').value;
1

You're looking for parseInt:

var myNum = parseInt(a, 10) + parseInt(b, 10);

You should always call this with its radix argument (, 10). Here's why.

MDN also lists it as a required parameter.

3
  • 2
    Do we -1 for you assuming the values are integers or do we comment?
    – user1850421
    Dec 17, 2013 at 14:05
  • @NigelAngel Don't get so touchy. The OP said they are integers anyway: the result is : 2+3=23 but i want 2+3=5 Dec 17, 2013 at 14:06
  • only in the example they gave.
    – user1850421
    Dec 17, 2013 at 14:06
1

+ adds numbers and concats strings. Since you haven't said that your numbers are integers, using parseInt() may not be correct, you should use parseFloat() to convert the strings to floats.

a = parseFloat(document.getElementById('Text1').value);
b = parseFloat(document.getElementById('Text2').value);

parseFloat()

1

It's concatenating the strings 2 and 3.

> '2' + '3'
"23"
> 2 + 3
5

You can get by this by using parseInt(numberVariable, 10):

> parseInt('2', 10) + parseInt('3', 10)
5

If you use decimal numbers, you can use parseFloat(numberVariable).

> parseFloat('2') + parseFloat('3')
5
1

First you need to convert them to integers, and then add them up:

a = parseInt(document.getElementById('Text1').value, 10);
b = parseInt(document.getElementById('Text2').value, 10);

If the value of the text boxes aren't integers, but floats, then use parseFloat.

8
  • -1 for lack of a radix parameter. It should always be set. Dec 17, 2013 at 14:03
  • 8
    +1, lack of radix parameter warrants a comment, not a -1.
    – user1850421
    Dec 17, 2013 at 14:04
  • @DannyBeckett: I added a radix parameter.
    – ProgramFOX
    Dec 17, 2013 at 14:05
  • 1
    @NigelAngel: no need for the +1, just don't vote. Dec 17, 2013 at 14:07
  • 1
    @Qantas94Heavy That's exactly my point too Dec 17, 2013 at 14:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.