0

I am trying to add two numbers whenever a keyup happens in the textboxes of those two numbers. I want it to use jquery so my addition code is these:

HTML:

<div class="container">
<h1>jQuery live Try</h1>
<form>
    <input type="text" placeholder="Number 1" class="num1 key">
    <input type="text" placeholder="Number 2" class="num2 key">
    <br><br>
    Sum: <input type="text" class="sum" readonly="readonly"><br><br><br>
</form>
</div>

Jquery:

$(document).ready(function(){
        $(".sum").val("0");
        $(".key").val("");

        function calc(){
        var $num1=$(".num1").val();
        var $num2=$(".num2").val();
        $(".sum").val($num1+$num2);
    }
    $(".key").keyup(function(){
        calc();
    });
});

But if i enter 1 and 2 it outputs 12 not 3. How can I make it output the sum?

The js fiddle

4
  • $(".sum").val(+$num1+$num2); Commented Mar 24, 2015 at 13:29
  • sorry but doesn't work Commented Mar 24, 2015 at 13:39
  • 1
    Strange, I don't know why that doesn't work, but this does : jsfiddle.net/qsnygcwg/5 Commented Mar 24, 2015 at 13:42
  • i can see now your fiddle works. i first added the + before val(+$num1+$num2) Commented Mar 24, 2015 at 13:57

4 Answers 4

4

use parseInt() to convert string to Number

function calc() {
    var $num1 = ($(".num1").val() != "" && !isNaN($(".num1").val())) ? parseInt($(".num1").val()) : 0;
    var $num2 = ($(".num2").val() != "" && !isNaN($(".num2").val())) ? parseInt($(".num2").val()) : 0;
    $(".sum").val($num1 + $num2);
}

Your overall code should look like this:

$(document).ready(function() {
    $(".sum").val("0");
    $(".key").val("");

    function calc() {

        var $num1 = ($.trim($(".num1").val()) != "" && !isNaN($(".num1").val())) ? parseInt($(".num1").val()) : 0;
        console.log($num1);
        var $num2 = ($.trim($(".num2").val()) != "" && !isNaN($(".num2").val())) ? parseInt($(".num2").val()) : 0;
        console.log($num2);
        $(".sum").val($num1 + $num2);
    }
    $(".key").keyup(function() {
        calc();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
	<h1>jQuery live Try</h1>
	<form>
		<input type="text" placeholder="Number 1" class="num1 key">
		<input type="text" placeholder="Number 2" class="num2 key">
		<br><br>
		Sum: <input type="text" class="sum" readonly="readonly"><br><br><br>
	</form>
</div>

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

6 Comments

perfect. thank you. but what happens if the textbox is empty? according to your conditional operator
If it's empty or NOT A NUMBER its value will be considered as a 0.
@BlaiseM. have also used $.trim() value incase of white-space
okay! now i can see: If num1 is not empty then parse int rather send 0
I was just explaining the way i understood your conditional operator. Thank you
|
1

the .val() method return a string, string 1 + string 2 = 12. So use the parseFloat method.

change

var $num1=$(".num1").val();
var $num2=$(".num2").val();

to

var $num1=parseFloat($(".num1").val());
var $num2=parseFloat($(".num2").val());

Comments

0

You should parse the string representation to Integer

var $num1=parseInt($(".num1").val());
var $num2=parseInt($(".num2").val());

1 Comment

0

The value returned from jQuery is a string so you have to cast your value into integers:

var $num1=parseInt($(".num1").val(), 10) || 0;
var $num2=parseInt($(".num2").val(), 10) || 0;
$(".sum").val($num1+$num2);

Demo

or use parseFloat() if you want to support floating numbers.

Another possibilty would be to use the Number()-function, as this will cast an empty string automatically:

var $num1=Number($(".num1").val());
var $num2=Number($(".num2").val());

Demo 2

Reference

parseInt()

Number()

1 Comment

Your demo shows NaN, as there's not check for blank values.

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.