Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLInputElement
  4. stepUp()

HTMLInputElement: stepUp() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

TheHTMLInputElement.stepUp() method increments the valueof a numeric type of<input> element by the value of thestep attribute, or thedefaultstep value if the step attribute is not explicitly set. The method,when invoked, increments thevalue by(step * n), wheren defaults to1 if not specified, andstep defaults to thedefault value forstep if not specified.

Input typeDefault step valueExample step declaration
date1 (day) 7 day (one week) increments:
<input type="date" min="2019-12-25" step="7">
month1 (month) 12 month (one year) increments:
<input type="month" min="2019-12" step="12">
week1 (week) Two week increments:
<input type="week" min="2019-W23" step="2">
time60 (seconds) 900 second (15 minute) increments:
<input type="time" min="09:00" step="900">
datetime-local1 (day) Same day of the week:
<input type="datetime-local" min="019-12-25T19:30" step="7">
number1 0.1 increments
<input type="number" min="0" step="0.1" max="10">
range1 Increments by 2:
<input type="range" min="0" step="2" max="10">

The method, when invoked, changes the form control's value by the value given in thestep attribute, multiplied by the parameter, within the constraints set onthe form control. The default value for the parameter, if no value is passed, is1. The method will not cause the value to exceed thesetmax value, or defythe constraints set by thestep attribute.

If the value before invoking thestepUp() method is invalid—for example,if it doesn't match the constraints set by the step attribute—invoking thestepUp() method will return a value that does match the form controlsconstraints.

If the form control is non time, date, or numeric in nature, and therefore does notsupport thestep attribute (see the list of supported input types in thetable above), or if the step value is set toany, anInvalidStateError exception is thrown.

Syntax

js
stepUp()stepUp(stepIncrement)

Parameters

stepIncrementOptional

A numeric value. If no parameter is passed,stepIncrement defaults to1.

Return value

None (undefined).

Examples

Click the button in this example to increment thenumber input type:

HTML

html
<p>  <label for="theNumber">    Enter a number between 0 and 400 that is divisible by 5:  </label>  <input type="number" step="5" min="0" max="400" /></p><p>  <label>    Enter how many values of step you would like to increment by or leave it    blank:  </label>  <input type="number" step="1" min="0" max="25" /></p><input type="button" value="Increment" />

JavaScript

js
/* make the button call the function */const button = document.getElementById("theButton");button.addEventListener("click", () => {  stepOnUp();});function stepOnUp() {  let input = document.getElementById("theNumber");  let val = document.getElementById("incrementInput").value;  if (val) {    /* increment with a parameter */    input.stepUp(val);  } else {    /* or without a parameter. Try it with 0 */    input.stepUp();  }}

CSS

css
input:invalid {  border: red solid 3px;}

Result

Note if you don't pass a parameter to thestepUp method, it defaults to1. Any other value is a multiplier of thestep attributevalue, which in this case is5. If you pass4 as thestepIncrement, the input willstepUp by4 * 5, or20. If the parameter is0, the numberwill not be incremented. The stepUp will not allow the input to out of range, in thiscase stopping when it reaches400, and rounding down any floats that arepassed as a parameter.

Try setting the step increment input to1.2. What happens when you invoke themethod?

Try setting the value to4, which is not valid. What happens when youinvoke the method?

Specifications

Specification
HTML
# dom-input-stepup-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp