3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HTMLで簡単に数値のプラスボタンとマイナスボタンを作る

Last updated at Posted at 2024-10-31

意外と知らない方が多いと思いますが、JavaScirptで自作しなくても簡単にできます。
便利ですね!

<a id="minus" class="number-control">-</a>
<input type="number" id="number" value="5.0" max="10" min="1" step="0.1" />
<a id="plus" class="number-control">+</a>
document.getElementById("minus").addEventListener("click", (event) => {
    document.getElementById("number").stepDown();
});
document.getElementById("plus").addEventListener("click", (event) => {
    document.getElementById("number").stepUp();
});

stepUpとstepDownは以下のtypeで使えます。

  • number
  • range
  • date
  • month
  • week
  • time

詳しくはこちら
https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement/stepUp

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?