Sliders are form fields that let the user select a specific value within a range of predefined values. Normally, they are shown as a slider that can be moved along an axis, between the minimal value and a second value which is the maximum value.
Sometimes, you can move the slider anywhere on the scale; other times you have to choose one of the predefined values on the scale.
They are said to be “simulated” when they do not use the default form elements included in HTML, namely the <input type=“range” />
fields. They are often used because they are more attractive.
The basic HTML code uses a standard HTML5 slider:
<form action="index.html" method="post"> <label for="sleep-quality" id="sleep-quality-label"> Are you satisfied with the quality of your sleep? (whole number, from 1 for “Very dissatisfied” to 5 for “Very satisfied") </label> <input type="range" id="sleep-quality" name="sleep-quality" step="1" value="3" min="1" max="5" /> <input type="submit" value="Submit information" /> </form>
If HTML5 is not supported by the user’s browser, and if JavaScript is not activated, a text field will be displayed by default. Without instructions, the user will not know the range of authorized values.
Without JavaScript, a standard form is shown to the user.
With JavaScript, the behaviour of simulated sliders must the same as those of standard sliders.
Essentially, the method consists of the following steps:
Going into more detail, the scripts must handle the following points:
display: none;
is applied to the <input type=“range” />
tags.<input type=“range” />
. In the example, a <div>
is used.empty
element is added in the DOM within the newly created container. In the example, a <span>
is used. This is the slider. tabindex=“0”
is applied to it.role=“slider”
is applied to the slider.aria-labelledby
is applied to the slider. The value of this attribute is retrieved from the id
attribute of the label
corresponding to the slider. aria-valuemin
is applied to the slider. This attribute value corresponds to the minimum value authorized for the slider. aria-valuemax
is applied to the slider. This attribute value corresponds to the maximum value authorized for the slider. aria-valuenow
is applied to the slider. This attribute value corresponds to the current value of the slider. This attribute value is updated whenever the slider changes position.aria-valuetext
is applied to the slider. This attribute value corresponds to the current value of the cursor, but in a format adapted to the internet user. In our example, this attribute could, for example, have the value aria-valuetext=“Very satisfied”
when the aria-valuenow=“5”
. As it is more easily understood, it is the first value, if available, that will be shown to users of assistive devices. This attribute value is updated whenever the slider changes position. Right arrow
or Up arrow
increases the value of the slider by one step.Left
arrow or Down arrow
decreases the value of the slider by one step.Home
key moves the slider to the minimal value of the slider.End
key moves the slider to the maximal value. Page up
key increases the value of the slider, by an arbitrary value, but by more than one step. Page down
key decreases the value of the slider, by an arbitrary value more than one step, and equal to the amount by pressing on the Page up
key.The resulting code in JavaScript is as follows:
<form action="index.html" method="post"> <label for="Sleep-quality" id="sleep-quality-label"> Are you satisfied with the quality of your sleep? (Whole number, from 1 for “Very dissatisfied” to 5 for “Very satisfied") </label> <input style="display: none;" type="range" id="sleep-quality" name="sleep-quality" step="1" value="3" min="1" max="5" /> <div id="slider-container"> <span id="slider-cursor" role="slider" tabindex="0" aria-labelledby="sleep-quality-label" aria-valuemin="1" aria-valuemax="5" aria-valuenow="5" aria-valuetext="Very satisfied"></span> </div> <input type="submit" value="Submit information" /> </form>