Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I don't know what part I got wrong I keep getting
JavaScript
NaN
Please tell me what part is not right.

JavaScript
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));
}
Posted

You should validate that both x and y are number string before attempting any number conversion and arithmetic operation, e.g.
C#
var sum = 0;
if(isNaN(x) || isNaN(y)){
       alert('not a number');
} else {
       sum = parseFloat(x) + parseFloat(y);
}
 
Share this answer
 
Your code is working fine. You can check it at: JsFiddle Link[^] If the code is still not working, please give us a demo page.
 
Share this answer
 
hi,

check the output coming from x and y variable.

Is the correct value to perform arithmetic operations.

it may be null value or "" or undefined
 
Share this answer
 
Comments
missMarie 9-Dec-14 2:38am    
i have a value="" like that in one of my codes, because it's value depends on a dropdown menu. so what will i do now?
mudgilsks 9-Dec-14 2:40am    
when you adding value consider '' as 0
take value in variable ,check if they '' when 0 then add values

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