Quantity Spin Button Example

About This Example

The following example uses the Spin Button Pattern to implement three quantity inputs.

Similar examples include:

Example

Guests
Must be between 1 and 8 1 to 8
Must be between 0 and 8 0 to 8
Must be between 0 and 12 0 to 12

Accessibility Features

Keyboard Support

The spin buttons provide the following keyboard support described in the Spin Button Pattern.

Key Function
Down Arrow Decreases value by 1.
Up Arrow Increases value by 1.
Home Decreases to minimum value.
End Increases to maximum value.
Standard single line text editing keys
  • Keys used for cursor movement and text manipulation, such as Delete and Shift + Right Arrow.
  • An HTML input with type="text" is used for the spin button so the browser will provide platform-specific editing keys.

Role, Property, State, and Tabindex Attributes

Role Attribute Element Usage
spinbutton input[type="text"] Identifies the input[type="text"] element as a spin button.
aria-valuenow="NUMBER" input[type="text"]
  • Indicates the current numeric value of the spin button.
  • Updated by JavaScript as users change the value of the spin button.
aria-valuemin="NUMBER" input[type="text"] Indicates the minimum allowed value for the spin button.
aria-valuemax="NUMBER" input[type="text"] Indicates the maximum allowed value for the spin button.
aria-invalid="true" input[type="text"] Indicates that the current value is invalid.
aria-errormessage="ID_REF" input[type="text"] Identifies the element that provides an error message for the spin button.
title="NAME_STRING" button Defines the accessible name for each increment and decrement button (Remove adult, Add adult, Remove kid, Add kid, Remove animal, and Add animal).
aria-controls="ID_REF" button Identifies the element whose value will be modified when the button is activated.
tabindex="-1" button Removes the increment and decrement buttons from the page Tab sequence while keeping them focusable so they can be accessed with touch-based and voice-based assistive technologies.
aria-disabled="true" button Set when the minimum or maximum value has been reached to inform assistive technologies that the button has been disabled.
aria-hidden="true" span For assistive technology users, hides the visible minus and plus characters in the increment and decrement buttons since they are symbolic of the superior button labels provided by the title attribute.
output
  • An element with an implicit role of status and an implicit aria-live value of polite.
  • Triggers a screen reader announcement of the output element’s updated content at the next graceful opportunity.
  • When either the increment or decrement button is pressed, the current value of the spin button is injected into the output element.
  • Its contents are emptied 2000 milliseconds after injection.

JavaScript and CSS Source Code

HTML Source Code

To copy the following HTML code, please open it in CodePen.