Why Do I Get An Error Message That .replace Is Not A Function?
I have this function: function countLitreKgSums(cProductIds){ var cLitreKgSums = new Array(); var cWeek = 0; for(i=0;i
Solution 1:
cLitreKgValue
might be a number at the point where you try to call replace
on it, not a string. In which case, the error is correct - numbers don't have a replace
method.
Solution 2:
Solution 3:
you can use the function String() i.e.; String(cLitreKgValue).replace .... String function: http://www.w3schools.com/jsref/jsref_string.asp
Solution 4:
While the other answers work (and are correct, numbers don't have .replace()
, it's a String
method), I think an overall structure change is better, like this:
$("#plan_table td[class='week']").each(function(){
cWeek = $(this).html();
var cLitreKgValue = $("input[name*='plan_table_week" + cWeek + "_prod" + cProductIds[i] + "_']").val();
if (cLitreKgValue !== "") {
cLitreKgValue = cLitreKgValue.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/\s/g, '');
cLitreKgSum += parseFloat(cLitreKgValue);
}
});
There's no reason to do all that work when you know it's 0
and doesn't affect the result, so if ""
means 0
and anything += 0
has no net effect, just skip it :)
Post a Comment for "Why Do I Get An Error Message That .replace Is Not A Function?"