Reputation: 25
This works :
var x = parseFloat($(this).val());
$(this).val(x.toFixed(2));
and not this :
$(this).val(parseFloat($(this).val())).toFixed(2);
I get "toFixed is not a function".
I don't understand why !
Upvotes: 0
Views: 96
Reputation: 171690
Another way to write this is with val(function)
$(this).val(function(_, currVal){
return parseFloat(currVal).toFixed(2);
});
Your problem in second version is the chaining is incorrect
Upvotes: 1
Reputation: 36703
That is because you are applying toFixed
on jQuery object and not the value. Just shift it inside one parenthesis.
$(this).val(parseFloat($(this).val()).toFixed(2));
Splitting up code to simpler and more understandable syntax is always a great idea though. Use a code minifier to take care of the build size.
Upvotes: 1