Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript adding integers

How do I add a simple integer to another integer in Javascript?

I'm getting NaN as the value for total.

<script type="text/javascript">
var total = 0;
document.getElementById("dds1").onkeyup = function() {
    total = total + parseInt(this.value,10);
    updateIt();

};

function updateIt() {
//tofixed(2)
    document.getElementById("mySpan").innerHTML = total;
}

But if I do the following:

total = parseInt(this.value,10);

then total has a value (an integer value).

like image 423
CodeGuy Avatar asked Dec 14 '25 08:12

CodeGuy


2 Answers

The problem is that you execute the addition read the input value on every keyup. If the user, for instance, presses BACKSPACE to clear the input, the value will be an empty string, which will result in NaN after parseInt. And once you have NaN (in your total variable), you cannot get rid of it anymore.

Try this:

document.getElementById('dds1').onkeyup = function() {

    var value = parseInt(this.value, 10);

    if ( !isNaN(value) ) {
        total += value;
        updateIt();    
    }

};

Here, you first check if the input value can be parsed as a number. If not, you just disregard it.


Another way of doing it would be this:

document.getElementById('dds1').onkeyup = function() {
    var value = parseInt(this.value, 10);

    isNaN(value) && return;

    total += value;
    updateIt();
};

Here, if you read an input value that cannot be converted into an number, you just return the function altogether.

like image 62
Šime Vidas Avatar answered Dec 16 '25 21:12

Šime Vidas


Here is the javascript to add integers. Good thing is that it won't throw any error even for blank spaces.

Javascript

<script language="javascript" type="text/javascript">

  function Add() {
    var a, b, c, d;
    a = parseInt(document.getElementById("txtFirstValue").value);

    //
    // If textbox value is null i.e empty, then the below mentioned if condition will 
    // come into picture and make the value to '0' to avoid errors.
    //

    if (isNaN(a) == true) { a = 0; }
    var b = parseInt(document.getElementById("txtSecondValue").value);

    if (isNaN(b) == true) { b = 0; }
    var c = parseInt(document.getElementById("txtThirdValue").value);

    if (isNaN(c) == true) { c = 0; }
    var d = parseInt(document.getElementById("txtFourthValue").value);

    if (isNaN(d) == true) { d = 0; }
    document.getElementById("txtTotal").value = a + b + c + d;
}
</script>

<!-- begin snippet: js hide: false -->

HTML
First Value: <asp:TextBox ID="txtFirstValue" runat="server"
                         onKeyUp="javascript:Add();"></asp:TextBox>

Second Value:<asp:TextBox ID="txtSecondValue" runat="server"
                         onKeyUp="javascript:Add();"></asp:TextBox>

Third Value:<asp:TextBox ID="txtThirdValue" rrunat="server" 
                         onKeyUp="javascript:Add();"><asp:TextBox>

Fourth Value:<asp:TextBox ID="txtFourthValue" runat="server" 
                         onKeyUp="javascript:Add();"></asp:TextBox>

Total = <asp:TextBox ID="txtTotal" runat="server" MaxLength="20" BackColor="#FFE0C0" 
                         Enabled="False" Font- Font-Bold="True" Font-Size="X-Large"> 
            </asp:TextBox>

Referred from : http://www.ittutorials.in/source/javascript/scf4/addition-of-multiple-integers-using-javascript.aspx

like image 28
Imtiyaz Avatar answered Dec 16 '25 21:12

Imtiyaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!