srishti srishti - 11 days ago 7
jQuery Question

spin event doesn't trigger at first time in jquery spinner

I have created a custom spinner which appends a string after a number.

var percent = 5.5%;
$.widget("ui.pcntspinner", $.ui.spinner, {
_format: function(value){
return value + " %";
},

_parse: function(value){
return parseInt(value);
}
});


$("#spinner").pcntspinner().val(percent);


The spinner has two events attached to it i.e. focusout and spin. focusout works fine in first call too, but spin does not gets triggered at first time. Only after any other event has been triggered does the spin gets called. Any idea to solve this problem?

Answer

You are extending the widget so spin won't work you need _spin

$.widget("ui.pcntspinner", $.ui.spinner, {
    _format: function(value){
        return value + " %";
    },

    _parse: function(value){
        return parseInt(value);
    },

    _spin: function( event, ui ) { 
       alert('value updated by :' + ui);

       if(validation){
         var result = this._super( event, ui ); 
         return result; //<-- important for generic functionality
       }else
       {
         //returning nothing would make value not change
       }
    }
});