大网数据广告活动
Web 标准与前端开发

HTMLInputElement: stepDown() method

· 52CE 我爱测

The **`HTMLInputElement.stepDown()`** 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.

HTML DOM

The HTMLInputElement.stepDown() 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 input/date, input/month, input/week, input/time, input/datetime-local, input/number, and input/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.


<input type="time" max="17:00" step="900" />


<input type="date" max="2019-12-25" step="7" />


<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.

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.

Syntax

stepDown()
stepDown(stepDecrement)

Parameters

  • stepDecrement
    • : A numeric value. If no parameter is passed, stepDecrement defaults to 1.

      If the value is a float, the value will decrement as if Math.floor(stepDecrement) was passed. If the value is negative, the value will be incremented instead of decremented.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown in one of the following cases:
      • if the method is not applicable to for the current type value,
      • if the element has no step value,
      • if the value cannot be converted to a number,
      • if the resulting value is above the max or below the min.

Examples

Click the button in this example to decrement the input/number input type:

HTML

<p>
  <label for="theNumber">
    Enter a number between 0 and 400 that is divisible by 5:
  </label>
  <input type="number" step="5" id="theNumber" min="0" max="400" />
</p>
<p>
  <label for="decrementButton">
    Enter how many values of step you would like to decrement by or leave it
    blank:
  </label>
  <input type="number" step="1" id="decrementInput" min="-2" max="15" />
</p>
<input type="button" value="Decrement" id="theButton" />

CSS

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

JavaScript

/* make the button call the function */
let button = document.getElementById("theButton");
button.addEventListener("click", () => {
  stepOnDown();
});

function stepOnDown() {
  let input = document.getElementById("theNumber");
  let val = document.getElementById("decrementInput").value;

  if (val) {
    // decrement with a parameter
    input.stepDown(val);
  } else {
    // or without a parameter. Try it with 0, 5, -2, etc.
    input.stepDown();
  }
}

Result

Examples

Note if you don't pass a parameter to the stepDown() method, it defaults to 1. Any other value is a multiplier of the step attribute value, which in this case is 5. If we pass 4 as the stepDecrement, the input will stepDown by 4 * 5, or 20. If the parameter is 0, the number will not be decremented. The stepDown() method will not allow the input to go out of range, in this case stopping when it reaches 0 and rounding down and floats that are passed as a parameter.

Try setting the step decrement input to 1.2. What happens when you invoke the method?

Try setting the value to 44, which is not valid. What happens when you invoke the method?

Specifications

Browser compatibility

See also

  • input
  • HTMLInputElement
  • HTMLInputElement.stepUp
  • step, min and max attributes

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。