0

This is the code:

<html>
<body>

<script>
function myFunction(var1,var2){
number=var1+var2
document.write(number)
}
</script>

<form>
Number 1 : <input type="text" name="no1"><br>
Number 2 : <input type="text" name="no2"><br>
<input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
</form>

<p id="demo></p

</body>
</html>

When I insert 10 for number 1 and 20 for number 2, the output is:

1020

But i want it to display 30. What can i do?

**I have tried myFunction(10,20), the result is 30.

3
  • 4
    Look up the parseInt method. Commented Feb 25, 2014 at 12:57
  • @EvanTrimboli: number=+var1+ +var2; also works, though it is hacky and less readable then parseInt() off course. Commented Feb 25, 2014 at 13:21
  • It will, but the OP seems to be just starting out, so less "magic" is better. Commented Feb 25, 2014 at 20:34

8 Answers 8

8

simply use parse the variable value to integer using parseInt() method or add "+"before to your variable name. Because variables var1 and var2 returning string. To calculate those variable values, you need to convert it as a integer.

using parseInt() method

number=parseInt(var1)+parseInt(var2)

use + before variable name to convert into integer,

number= +var1 + +var2

try this code,

<html>
    <body>
        <script>
        function myFunction(var1,var2){
            number = parseInt(var1) + parseInt(var2)
            //another way number= +var1+ +var2
            document.write(number)
        }
        </script>
    <form>
        Number 1 : <input type="text" name="no1"><br>
        Number 2 : <input type="text" name="no2"><br>
        <input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
    </form>

    <p id="demo"></p>

    </body>
</html>

using parseInt() DEMO

using + before variable name DEMO

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

1 Comment

+1 and just for the sake of mentioning it, a short-hand implicit cast would be: number=+var1+ +var2; (space between + signs is important) i.e: "2" + "3" is "23", while +"2"+ +"3" is 5. Though parseInt() is off course the right way to go.
2

modify your function with parseInt like:

<script>
    function myFunction(var1,var2){
        number=parseInt(var1)+parseInt(var2);
        document.write(number);
    }
</script>

You were getting output like 1020 because by default data from the textbox is taken as text type, so we need to convert it to Number Type first, for that we are using parseInt(for explicit conversion)

Comments

2

Your javascript thinks you are appending strings... To make sure your javascript knows it's numbers your working with you need to convert it to that type.

<html>
    <body>
        <script>
        function myFunction(var1, var2){
            number = parseInt(var1, 10) + parseInt(var2, 10)
            document.write(number)
        }
        </script>
        <form>
            Number 1 : <input type="text" name="no1"><br>
            Number 2 : <input type="text" name="no2"><br>
            <input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
        </form>

        <p id="demo"></p>
    </body>
</html>

For more info about parseInt check this documentation.

Comments

2

Update your method to

function myFunction(var1,var2){
  number=parseInt(var1) + parseInt(var2)
  document.write(number)
}

As this.form.no1.value is returning a string, so both the numbers are concatenated as strings instead of summing up as numbers.

Comments

1

Two options:

Change your input tag to

<input type="button" onclick="myFunction(parseInt(this.form.no1.value, 10),parseInt(this.form.no2.value, 10))" value="submit">

OR

Change your JavaScript function to

    function myFunction(var1,var2){
      var number=parseInt(var1, 10)+parseInt(var2, 10);
      document.write(number);
    }

Comments

1

It is because the values you extract from your input fields are strings. When you add two strings, they are usually concatenated. Try looking at the javascript method parseIntas Evan suggests in the comments or look at parseFloatif you want to allow floats.

parseFloat docs

Your method would then look like this:

function myFunction(var1,var2){
    number = parseFloat(var1) + parseFloat(var2)
    document.write(number)
}

Comments

1

It's now just string concatenation. Please use "parseInt()" to get the result.

thanks.

Comments

1

Your not doing a calculation, you are appending two Strings. In order to calculate the mathematical answer for var1 + var2 you should parse them to Integers.

result = parseInt(var1) + parseInt(var2);

1 Comment

Ok, I will at least give +1s to other people, because more people came up with the same answer. Thanks.

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.