Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
These are my codes. I don't know why I keep getting NaN? Please help me on this one.

JS
JavaScript
function subtotal(){
            var as = $("#as").val();
            var txt1 = $('#sff').val();
            var txt2 = $('#osf').val();
            var d = 0;
            var e = 0;
            var f = 0;
            var g = 0;
            
            var a = parseFloat(as, 10);
            var b = parseFloat(txt1, 10);
            var c = parseFloat(txt2, 10);
            
            if ($('#cbx3').is(":checked")) {
                d = parseFloat($("#cbx3").val(), 10);
            }
            if ($('#cbx4').is(":checked")) {
                e = parseFloat($("#cbx4").val(), 10);
            }
            if ($('#cbx5').is(":checked")) {
                f = parseFloat($("#cbx5").val(), 10);
            }
            if ($('#cbx6').is(":checked")) {
                g = parseFloat($("#cbx6").val(), 10);
            }
            var total = a + b + c + d + e + f + g;
             
            $('#st').val(total.toFixed(2)); 
}
function grandtotal(){
    var x = document.getElementById("st").value;
    var y = document.getElementById("shppng").value;
    
    var sum = parseFloat(x, 10)+ parseFloat(y, 10);
    $("#gt").val(sum.toFixed(2));
}


HTML
<input type="text" id="sff" placeholder="$0.00" value="" style="float:left;"/>
<input type="text" id="osf" placeholder="$0.00" value="" style="float:left;"/>
<select id="as" style="margin: 3px 0 0 55px; width:48%; position: absolute; float: left;">
                                       
<option value="99.00">$99.00/</option>
                    
<option value="178.20">$178.20</option>
       
<option value="241.56">$241.56</option>
       
<option value="292.24">$292.24</option>
      
<option value="332.80">$332.80</option>
      
</select>
<input type="checkbox" id="cbx4" value="69.00"/>
               
<input type="checkbox" id="cbx5" value="35.00"/>
               
<input type="checkbox" id="cbx6" value="39.00"/>
Posted
Comments
/\jmot 9-Dec-14 3:53am    
not clear?
specify..
Kornfeld Eliyahu Peter 9-Dec-14 3:57am    
Because one of the values can not be converted to number using parseFloat...Go and debug your code to see on what line you have the problem...

hi marrie,

also declare var a,b,c

before adding check
if (a=='' || a=='undefind'){
a=0
}

similarly other variable after that add them,it might sol your problem.
 
Share this answer
 
The code does not show where subtotal or grandtotal are being called.

But the following advice should help you get you going on getting the code to work :)

NaN stands for not a number.

http://www.w3schools.com/jsref/jsref_parsefloat.asp[^]
Note: If the first character cannot be converted to a number, parseFloat returns NaN.

I am assuming the $ character is the first character being checked as that is the format specified to be used for the input, also an empty string is NaN.
JavaScript
//modify subtotal to validate the data or create an extra dedicated function for validation to be called within subtotal
function subtotal(){
	var as = $("#as").val();
	var txt1 = $('#sff').val();
	var txt2 = $('#osf').val();
	var d = 0;
	var e = 0;
	var f = 0;
	var g = 0;
//place code here to validate the data to ensure they are in a numeric format
//and/or re-format the data to a numeric format
	var a = parseFloat(as, 10);
	var b = parseFloat(txt1, 10);
	var c = parseFloat(txt2, 10);
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900