A spin box or spinner is a user interface widget consisting of a numeric entry field combined with buttons to increment and decrement the displayed value. SpinBox is a JavaScript object that converts text input fields into spin boxes. The value can be changed using the spinner buttons or by typing, using the up and down arrow keys, or using the mouse wheel when the field has the focus. The examples below show the various types of spin boxes that can be created.
Basic spin box: | |
Spin box with limited range: | |
Spin box with decimal places: | |
Disabled spin box: |
Download SpinBox
Download one of the files below and either incorporate it into your code or serve it as a separate file.
File | Size | Description |
---|---|---|
SpinBox.js | 3,568 bytes | Minified version |
SpinBox.src.js | 9,792 bytes | Full version, with comments |
SpinBox does not apply any styling to the spin boxes. The archives below contain a stylesheet and images that give spin boxes the same appearance as those in the examples above.
File | Size | Description |
---|---|---|
stylesheet.zip | 1,113 bytes | Zip archive |
stylesheet.7z | 932 bytes | 7-Zip archive |
Creating spin boxes
The spin box is created within an HTML element. The element can either be empty, or contain a text input field — for example:
1 2 3 |
|
By putting a text input field within the containing element the user will be able to enter a value even if JavaScript is not available. This fallback functionality is important if the spin box is used to enhance a normal web page, but is not required inside a full JavaScript application.
To convert a text input field into a spin box, a new instance of SpinBox should be created. Either the ID of the containing element, or the DOM node corresponding to it, should be passed as a parameter. For example:
1 2 3 4 5 |
|
Spin box options
The SpinBox constructor can take a second parameter consisting of an object setting various options. As many or as few options as required can be specified.
By default SpinBox.js sets a class of spinBox
on the container, and classes of spinnerUp
and spinnerDown
on the buttons.
The className
option sets an alternative class to use on the container, and with suffixes of Up
and Down
on the buttons:
1 2 |
|
If the container was empty before the spin box was created, the new input field will default to a value of zero.
The value
option sets the initial value for the input field (regardless of whether the container was empty):
1 2 |
|
The spinner buttons will increase or decrease the value by 1 by default.
The step
option sets an alternative step size:
1 2 |
|
There are no limits on the value by default.
The minimum
and maximum
options can be used to restrict the value:
1 2 3 4 5 6 7 8 |
|
The spin box only allows whole numbers by default.
To override this behaviour, use the decimals
option to set the number of decimal places displayed:
1 2 |
|
Note that the minimum, maximum, and decimal places restrictions are enforced for values set by the spin box, but a value outside of these restrictions may be typed by the user.
Getting and setting values
The getValue
function provides access to the current value:
1 2 |
|
This will be a number, or the value NaN if the current contents of the input field do not start with a valid number.
Use the isNaN
function to distinguish between the two cases.
The setValue
function sets the value, while enforcing any minimum, maximum, and decimal places restrictions:
1 2 |
|