Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am getting value as NaN.when i try to blank my textbox .why i am getting this.

What I have tried:

THIS IS MY SCRIPT:

$("#txtSC").keyup(function () {


var myBox1 = document.getElementById('box1').value;
var myBox2 = document.getElementById('box2').value;
var mybox3 = document.getElementById('box3').value;
var mybox4 = document.getElementById('txtDiscount').value;
var mybox5 = document.getElementById('txtSC').value;

var multply = myBox1 * myBox2;
var per = (multply * mybox3) / 100;
var myResult = multply + per;
var withDisCount = (myResult * mybox4) / 100;
var myDiscount = myResult - withDisCount;
var ShippingCost = myDiscount + (parseFloat(mybox5));

// var finalResult = Math.round(ShippingCost);
var result = document.getElementById('TotalAmt');

result.value = ShippingCost;




});


on last textbox i.e mybox5, when i am trying to backspace in that textbox(making it empty manually) my "TotalAmt" textbox show NaN value.
Posted
Updated 17-Jun-16 3:00am

1 solution

Make sense, because what you do is this:
var myDiscount = myResult - withDisCount;
and that will be a number for example:
100.
and then you try to sum it with (parseFloat(mybox5));.
However your mybox5 is empty. And result of parseFloat('') is NaN.
so you will make
100 + NaN
which is NaN

make sense?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jun-16 9:30am    
My 5.
Absolutely. NaN is not a "problem", but a valid object in this and many other cases. It's just the opposite: if parsing of empty string, or null, or undefined, or invalid string returned 0 (as many thought they wanted), that would be a real problem.
—SA

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