javascriptjqueryjquery-uijquery-ui-spinner

Disable holding the up and down buttons on a jQueryUI spinner


I'm using jQuery 1.7.1 and jQueryUI 1.9.1.

I have a spinner, and every time it changes, a text field will be created or removed to match the number on the spinner. Holding the button will cause the number to change very rapidly, causing a ton of fields to be created or removed.

Not a huge problem since it's client-side, but I just don't like it. So I want to disable the rapid spinning when the user holds the spinner buttons.

I came up with a solution using a function for incremental, which looks like this:

var incrementalFunction = function(numOfSpins) {
    if (numOfSpins == 1) {
        return 1;
    }
    return 0;
};

This worked great at first, but caused another issue. Next to each newly created text box, I made a 'remove' button that would remove the element and decrement the spinner. But when I call the stepDown method, for some reason, this calls my incremental function, with an increasing numOfSpins every time it was called. So it would only decrement once.

Anyone have a more straightforward solution to preventing the user from holding the increment/decrement buttons (or the up/down arrows on the keyboard)?


Solution

  • If you upgrade to jQuery UI 1.10, the problem will go away. See https://github.com/jquery/jquery-ui/commit/0d53fbfd0b7651652601b3b8577225ab753aab44 which causes stepUp() and stepdDown() to behave as you'd expect.