SYNCRo SYNCRo - 1 year ago 168
jQuery Question

jQuery UI spinner onchage return old value

Considering the following snippet

<input type="text" id="age" value="" maxlength="2" style="width: 30px;" />
<script type="text/javascript">
$("#age").spinner({
min: 1,
max: 99,
spin: function(event, ui) {
$(this).change();
}
}).val(35);
$("#age").change(function() {
console.log($("#age").val());
});
</script>


Why when
onchange
event occurs by pressing Up/Down key or clicking the spinner it log the old spinner value?

JS Fiddle

Answer Source

The plugin takes some time to update the value. Try using the build in callback, ref: http://api.jqueryui.com/spinner/#event-spin

as a seperate event.

$("#age").on( "spin", function( event, ui ) { 
    console.log(ui.value)
});

or as a option on init

$("#age").spinner({
    min: 1,
    max: 99,
    spin: function( event, ui ) {
        console.log(ui.value)
    }
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download