ober Posted June 22, 2010 Share Posted June 22, 2010 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?? Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/ Share on other sites More sharing options...
premiso Posted June 22, 2010 Share Posted June 22, 2010 The plus operator tends to concat in javascript. Perhaps try to convert the value to a float before adding, using parseFloat Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1075805 Share on other sites More sharing options...
ober Posted June 23, 2010 Author Share Posted June 23, 2010 I was under the impression that unless you were using strings (which I'm not), it worked fine as an addition. I also trying wrapping the values in the Number() function and that resulted in a NaN result. Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1075878 Share on other sites More sharing options...
buddhika2010 Posted June 23, 2010 Share Posted June 23, 2010 put eval( documentgetElementID......) it should work ........or else u send me the code i fix it buddika@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076000 Share on other sites More sharing options...
ober Posted June 23, 2010 Author Share Posted June 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076072 Share on other sites More sharing options...
ober Posted June 23, 2010 Author Share Posted June 23, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076118 Share on other sites More sharing options...
ober Posted June 23, 2010 Author Share Posted June 23, 2010 Ok... got it figured out. I was using PHP's number_format() function to format the "billtotalx" value and it was throwing a comma into the value, which was causing it not to be evaluated as a number. DAMN. Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076125 Share on other sites More sharing options...
Maq Posted June 23, 2010 Share Posted June 23, 2010 That's loosely typed languages for you.... Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076169 Share on other sites More sharing options...
ober Posted June 24, 2010 Author Share Posted June 24, 2010 Umm... ok, but I already solved it so I'm not sure why you're replying. Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076590 Share on other sites More sharing options...
Maq Posted June 24, 2010 Share Posted June 24, 2010 Umm... ok, but I already solved it so I'm not sure why you're replying. They're spamming themselves for freelance work. Quote Link to comment https://forums.phpfreaks.com/topic/205586-addition-not-working/#findComment-1076629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.