Jump to content

Addition not working


ober

Recommended Posts

billtotal = 30,000.00 at the start

 

I have this:

var totalboxes = document.getElementById('boxcount').value;
    var billtotal = document.getElementById('billtotalx').value;
    var billtotalbox = document.getElementById('billtotal');
    
    var i=0;
    for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null)
        {
            billtotal += document.getElementById('bill'+i).value;
            alert(billtotal);
        }
    }

 

I have 8 fields that are being added to an existing total.  This is an onkeyup function and the alert shows this as it loops:

 

30,000.000

30,000.0005

30,000.00050

30,000.000500

30,000.0005000

30,000.0005000

30,000.0005000

30,000.0005000

 

The fields that it is adding contains:

 

0

5

0

0

null

null

null

null

 

So it looks like it is appending instead of adding.  What am I doing wrong??

Link to comment
https://forums.phpfreaks.com/topic/205586-addition-not-working/
Share on other sites

Anyone else have any ideas?  parseFloat triggers a result of NaN.  Using eval doesn't change anything.

 

Here is the entire function:

 

function calculate_ger(_box)
{
    // ger calculation including totals for GER and Billable units
    var gerbox = document.getElementById('ger'+_box);
    var billbox = document.getElementById('bill'+_box);
    var ratebox = document.getElementById('price_rate');
    var retval = billbox.value * ratebox.value;
    gerbox.disabled = "false";
    gerbox.value = retval.toFixed(2);
    gerbox.disabled = "true";
    
    // billable units total calculation
    
    var billtotal = 0;
    var totalboxes = document.getElementById('boxcount').value;
    var billtotal = parseFloat(document.getElementById('billtotalx').value);
    var billtotalbox = document.getElementById('billtotal');
    
    var i=0;
    for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null)
        {
            billtotal += parseFloat(document.getElementById('bill'+i).value);
            //alert(billtotal);
        }
    }
    
    billtotalbox.disabled = "false";
    billtotalbox.value = billtotal;
    billtotalbox.disabled = "true";
    
}

 

The first part works great.  I don't understand why the second part isn't.

WTF...

for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null && document.getElementById('bill'+i).value != '')
        {
            total += Number(document.getElementById('bill'+i).value);
        }
    }
    alert(billtotal);
    total = total*1 + billtotal*1;
    alert(total);

 

The first alert gives me the number.  The loop is working fine, but when I try to add the two together, I get "NaN".  This is insane!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.