Skip to content Skip to sidebar Skip to footer

Javascript - NaN

I am currently trying to do a small calculation to find the Markup of a product. How ever I am getting a 'NaN' error in my console. Obviously I know this means Not a Number but I c

Solution 1:

jQuery val is a function. You need () to call a function:

var idealGP = $('#bc_inventorybundle_dish_ideal_gp').val();
var cost = $('#bc_inventorybundle_dish_cost').val();

Also, when reading numbers from an external source, it is best to convert to numbers and do validation immediately, not during your calculation:

var idealGP = parseFloat($('#bc_inventorybundle_dish_ideal_gp').val());
var cost = parseFloat($('#bc_inventorybundle_dish_cost').val());

suggestedCost = cost /(1.0 - idealGP); // now you can assume that everything is numbers.

Solution 2:

when your are trying to get the value from some it or so use .val(). not val

 var idealGP = $('#bc_inventorybundle_dish_ideal_gp').val();

Post a Comment for "Javascript - NaN"