The HTMLInputElement.stepDown([n])
method decrements the value of a numeric type of <input>
element by the value of the step
attribute or up to n
multiples of the step attribute if a number is passed as the parameter.
The method, when invoked, decrements the value
by (step
* n), where n defaults to 1 if not specified, and step
defaults to the default value for step
if not specified.
Valid on all numeric, date, and time input types that support the step attribute, including date, month, week, time, datetime-local, number, and range.
Given <input id="myTime" type="time" max="17:00" step="900" value="17:00">
, invoking myTime.stepDown(3)
will change the value to 16:15, decrementing the time by 3 * 900
, or 45 minutes. myTime.stepDown()
, with no parameter, would have resulted in 16:45
, as n
defaults to 1
.
html
<!-- decrements by intervals of 900 seconds (15 minute) --> <input type="time" max="17:00" step="900" /> <!-- decrements by intervals of 7 days (one week) --> <input type="date" max="2019-12-25" step="7" /> <!-- decrements by intervals of 12 months (one year) --> <input type="month" max="2019-12" step="12" />
However, calling stepDown
on <input type="time" max="17:00" step="900">
would not set the value to 17:00
, as one would expect — and as it does for stepUp
when the input is <input type="time" min="17:00" step="900">
. Instead, the first call to stepDown
will set the initial value to 23:45
even though the max
attribute is set. The second call will set the value to 17:00
. And the third call to will set the value to 16:45
.
js
let input1 = document.createElement("input"); input1.setAttribute("type", "time"); input1.setAttribute("min", "17:00"); input1.setAttribute("step", 900); console.log(input1.value); // "" input1.stepUp(); console.log(input1.value); // "17:00" // However let input2 = document.createElement("input"); input2.setAttribute("type", "time"); input2.setAttribute("max", "17:00"); input2.setAttribute("step", 900); console.log(input2.value); // "" input2.stepDown(); console.log(input2.value); // "23:45" input2.stepDown(); console.log(input2.value); // "17:00" input2.stepDown(); console.log(input2.value); // "16:45"
The method, when invoked, changes the form control's value by the value given in the step
attribute, multiplied by the parameter, within the constraints set within the form control. The default value for the parameter, if not is passed, is 1. The method will not cause the value to go below the min
value set or defy the constraints set by the step
attribute. A negative value for n
will increment the value, but will not increment beyond the max
value.
If the value before invoking the stepDown()
method is invalid, for example, if it doesn't match the constraints set by the step
attribute, invoking the stepDown()
method will return a value that does match the form controls constraints.
If the form control is non time, date, or numeric in nature, and therefore does not support the step
attribute (see the list of supported input types above), or if the step
value is set to any
, an InvalidStateError
exception is thrown.
HTMLInputElement.stepDown()
-
Decrements the
value
by (step
* n), where n defaults to 1 if not specified. Throws anInvalidStateError
exception: