lint warning that val() and text() are called incorrectly

313 views Asked by At

I have:

 TotalPrice = parseInt(TotalPrice*100)/100;
 $('input[name=EstimatedPrice]').val(TotalPrice);
 $('#EstimatedPriceDisplay').text(TotalPrice);

and I'm getting two warnings from lint.

  1. val() called incorrectly

  2. text() called incorrectly.

I was able to eliminate the text() called incorrectly error by doing the following:

$('#EstimatedPriceDisplay').text('' + TotalPrice);

But that seems kinda kludgy to me.

4

There are 4 answers

1
kennytm On BEST ANSWER

Do you want to support IE <5.5? If not, try to use the .toFixed() method, which returns a string and rounded to the specified decimal place.

 TotalPrice = TotalPrice.toFixed(2);
0
Sarfraz On

Doing:

$('#EstimatedPriceDisplay').text('' + TotalPrice);

should be fine or you can use the toString method:

$('#EstimatedPriceDisplay').text(TotalPrice.toString());
1
user113716 On

Since you're reusing the variable, I'd make it a String when the value is assigned.

TotalPrice = (Math.round(TotalPrice*100)/100) + '';

This way you only need to do it once, and it is not cluttering .val() and .text()

$('input[name=EstimatedPrice]').val(TotalPrice);
$('#EstimatedPriceDisplay').text(TotalPrice);

EDIT: Changed to Math.round() to get proper up/down rounding.

0
Phillip Senn On
 $('input[name=EstimatedPrice]').val(TotalPrice.toFixed(2));
 $('#EstimatedPriceDisplay').text(TotalPrice.toFixed(2));