0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScropt [初心者の備忘録] テキストボックスに入力した数字で加減するカウンター

Posted at

[やりたかったこと]
テキストボックスを作成し、そこに入力された数値に対して、
プラスまたはマイナスボタンをクリックすると、その分だけ加算または減算されるだけのもの。

<div class="spinner_area">
      <p id="counter" type="number">0</p>
      <input type="button" value="+" class="btnspinner" data-cal="1" data-target=".counter1" id="up" hovercolor="red">
      <input type="number" value="1" class="counter1" data-max="500" data-min="0" id="textbox">
      <input type="button" value="-" class="btnspinner" data-cal="-1" data-target=".counter1" id="down" hovercolor="red">

</div>
window.addEventListener('DOMContentLoaded', ()=>{
  const downbutton = document.querySelector('#down');
  const upbutton = document.querySelector('#up');
  const text = document.querySelector('#textbox');
  const counter = document.querySelector('#counter');
  downbutton.addEventListener('click', (event) => {
    counter.textContent=parseInt(counter.textContent)-parseInt(text.value);
  });
  upbutton.addEventListener('click', (event) => {
    counter.textContent=parseInt(counter.textContent)+parseInt(text.value);;
  });
});
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?