Question : Jquery Number field Roundoff

Hi

We are using following code to roundoff the input field at our forms

  $(".roundoff2d").bind("change blur",function(){
     if(!($.trim($(this).val()) == '')  &&  !(isNaN($.trim($(this).val())))){
       $(this).val(( parseFloat( $.trim( $(this).val() ) ).toFixed(2) ));
     }
     if(isNaN($.trim($(this).val()))){$(this).focus().val('');}
     return false;
  });

We add class="roundoff2d" with the input fields we want to round.

This works fine when a value an entered in the field and tab is pressed. But I also want some automatic functionality that when the page is open the values should be rounded off.

Please help. I want all the fields with class="roundoff2d" should be rounded off when the page is open.

Answer : Jquery Number field Roundoff

Hi,

If by "when the page is open" you mean when it first loads up in the browser and the input fields may have non-rounded values in then you could use a document ready event to fire the blur event on each of the appropriate elements :

$(document).ready(function() { $(".roundoff2d").blur() ;});

If you mean something else then I'd be happy to help if you can clarify further!

Hope that's of use!
Random Solutions  
 
programming4us programming4us