The HTMLInputElement.stepUp()
method increments the value of a numeric type of <input>
element by the value of the step
attribute, or the default step
value if the step attribute is not explicitly set. The method, when invoked, increments 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.
Input type | Default step value | Example step declaration |
---|---|---|
date | 1 (day)
|
7 day (one week) increments:
|
month | 1 (month)
|
12 month (one year) increments:
|
week | 1 (week)
|
Two week increments:
|
time | 60 (seconds)
|
900 second (15 minute) increments:
|
datetime-local | 1 (day)
|
Same day of the week:
|
number | 1
|
0.1 increments
|
range | 1
|
Increments by 2:
|
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 on the form control. The default value for the parameter, if no value is passed, is 1
. The method will not cause the value to exceed the set max
value, or defy the constraints set by the step
attribute.
If the value before invoking the stepUp()
method is invalid—for example, if it doesn't match the constraints set by the step attribute—invoking the stepUp()
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 in the the table above), or if the step value is set to any
, an InvalidStateError
exception is thrown.
Syntax
element.stepUp( [ stepIncrement ] );
Parameters
stepIncrement
- The optional
stepIncrement
paremeter is a numeric value. If no parameter is passed,stepIncrement
defaults to1
.
Example
Click the button in this example to increment the number input type:
HTML
<p>
<label>Enter a number between 0 and 400 that is divisible by 5:
<input type="number" step="5" id="theNumber" min="0" max="400">
</label>
</p>
<p>
<label>Enter how many values of step you would like to increment by or leave it blank:
<input type="number" step="1" id="incrementer" min="0" max="25">
</label>
</p>
<input type="button" value="Increment" id="theButton">
JavaScript
/* make the button call the function */
let button = document.getElementById('theButton')
button.addEventListener('click', function() {
steponup()
})
function steponup() {
let input = document.getElementById('theNumber')
let val = document.getElementById('incrementer').value
if (val) { /* increment with a parameter */
input.stepUp(val)
} else { /* or without a parameter. Try it with 0 */
input.stepUp()
}
}
CSS
input:invalid {
border: red solid 3px;
}
Result
Note if you don't pass a parameter to the stepUp
method, it defaults to 1
. Any other value is a multiplier of the step
attribute value, which in this case is 5
. If you pass 4
as the stepIncrement
, the input will stepUp
by 4 * 5
, or 20
. If the parameter is 0
, the number will not be incremented. The stepUp will not allow the input to out of range, in this case stopping when it reaches 400
, and rounding down any floats that are passed as a parameter.
Try setting the step increment to 1.2
. What happens when you invoke the method?
Try setting the value to 4
, which is not valid. What happens when you invoke the method?
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living StandardThe definition of 'stepUp()' in that specification. | Living Standard | |
HTML 5.1The definition of 'stepUp()' in that specification. | Recommendation | |
HTML5The definition of 'stepUp()' in that specification. | Recommendation | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
stepUp
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox Full support 16 Full support 16 Notes' Experimental, and without specific UI. There are still differences with the latest spec: bug 835773.
Disabled' From version 16: this feature is behind the |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
See also
<input>
HTMLInputElement
HTMLInputElement.stepDown
step
,min
andmax
attributes
HTMLInputElement.stepUp() by Mozilla Contributors is licensed under CC-BY-SA 2.5.