Dman100
Dman100

Reputation: 747

set value toFixed on blur

I have an input field that is used to insert a dollar amount. If the user types in "100" or "100." I want to add two decimal places to the value using the toFixed(2) method.

Here is an example

This is what I have tried:

var j$ = jQuery.noConflict();

j$(document).ready(function() {
    j$('#ccAmt').blur(function() {
        j$(this).val(j$(this).val().toFixed(2));
    });
});

I found several examples on the forum, but everything I tried didn't work. What am I missing?

Thanks.

Upvotes: 1

Views: 557

Answers (2)

Roko C. Buljan
Roko C. Buljan

Reputation: 206514

jsBin demo

var j$ = jQuery.noConflict();


j$(document).ready(function($) {

    $('#ccAmt').blur(function() {
        var val = parseFloat( this.value ).toFixed(2);
        $(this).val( val );
    });

})(jQuery);

Upvotes: 2

Jason Whitted
Jason Whitted

Reputation: 4024

Is .val() returning a Number? Try:

j$(this).val(parseFloat(j$(this).val()).toFixed(2));

Upvotes: 4

Related Questions