押されたボタンの番号を変数に代入したい\r\n
\r\n```","answerCount":3,"upvoteCount":0,"datePublished":"2020-10-07T22:21:22.008Z","dateModified":"2020-10-07T22:21:22.008Z","acceptedAnswer":{"@type":"Answer","text":"```javascript\r\nconst btns = document.querySelectorAll(\".btn\");\r\nbtns.forEach((x,y)=>{\r\n  x.addEventListener('click',()=>{\r\n    console.log(y);\r\n  });\r\n});\r\n```","dateModified":"2020-10-08T00:13:06.828Z","datePublished":"2020-10-08T00:13:06.828Z","upvoteCount":2,"url":"https://teratail.com/questions/296601#reply-418869"},"suggestedAnswer":[{"@type":"Answer","text":"ググればいろいろ見つかりますよ。\r\n\r\n[js addEventListener 何番目がクリックされたか - Google 検索](https://www.google.com/search?sxsrf=ALeKk01IVra9gKxmSG4OMOMHixep5KI-dw%3A1602115516783&ei=vFd-X4GmL82noASy6puwBA&q=js+addEventListener+%E4%BD%95%E7%95%AA%E7%9B%AE%E3%81%8C%E3%82%AF%E3%83%AA%E3%83%83%E3%82%AF%E3%81%95%E3%82%8C%E3%81%9F%E3%81%8B&oq=js+addEventListener+%E4%BD%95%E7%95%AA%E7%9B%AE%E3%81%8C%E3%82%AF%E3%83%AA%E3%83%83%E3%82%AF%E3%81%95%E3%82%8C%E3%81%9F%E3%81%8B&gs_lcp=CgZwc3ktYWIQDDIECCMQJzoHCCMQsAMQJ1DhE1jhE2DROWgBcAB4AIABVogBVpIBATGYAQCgAQGqAQdnd3Mtd2l6yAEBwAEB&sclient=psy-ab&ved=0ahUKEwiB9JCy2aPsAhXNE4gKHTL1BkYQ4dUDCA0)","dateModified":"2020-10-08T00:11:35.049Z","datePublished":"2020-10-08T00:11:35.049Z","upvoteCount":1,"url":"https://teratail.com/questions/296601#reply-418868","comment":[{"@type":"Comment","text":"ご回答ありがとうございます。\r\n私のググり力が足りなかったということですね。精進します。","datePublished":"2020-10-08T02:40:19.717Z","dateModified":"2020-10-08T02:40:19.717Z"}]},{"@type":"Answer","text":"引数として渡すという形でしょうかね。\r\n[addEventListenerで関数に引数を渡す](https://qiita.com/rukiadia/items/03aaa8955c0f6576a5e7)","dateModified":"2020-10-07T22:29:11.184Z","datePublished":"2020-10-07T22:29:11.184Z","upvoteCount":2,"url":"https://teratail.com/questions/296601#reply-418850","comment":[{"@type":"Comment","text":"ご回答ありがとうございます。こんな方法もあるのですね。\r\n私の知識ではリンク先の情報を今回の質問内容に応用することは\r\n出来なさそうですが、勉強になります。","datePublished":"2020-10-08T02:40:11.784Z","dateModified":"2020-10-08T02:40:11.784Z"},{"@type":"Comment","text":"試さなければ身に付くものはなにもないと思います。\r\nそのまま動くコードもらっても理解できずに質問を繰り返すだけです。成長は見込めません。\r\n自分で解決できたらそれが一番早いのです","datePublished":"2020-10-08T03:57:48.063Z","dateModified":"2020-10-08T03:57:48.063Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/JavaScript","name":"JavaScriptに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/296601","name":"押されたボタンの番号を変数に代入したい"}}]}}}
HTMLで複数のボタンを設置し、1番目のボタンが押されたら0という数字を変数に代入し、2番目のボタンが押されたら1を代入したいのですが、上手くいきません。
let btns = document.querySelectorAll(".btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", test);
}
function test() {
console.log(i);
}
例えばボタンが3個あり、2番目のボタンを押した時、for文2行目のbtns[i]ではおそらくi=1なのですが、呼び出した先のfunctionではiには3が代入されてしまっています。
i=1のままfunctionを実行するにはどう書けば良いのでしょうか。
<html><body>
    <button class="btn">ボタン1</button>
    <button class="btn">ボタン2</button>
    <button class="btn">ボタン3</button>
<script>
  let btns = document.querySelectorAll(".btn");
  for (var i = 0; i < btns.length; i++) {
    btns[i].addEventListener("click", test);
  }
  function test() {
    console.log(i);
  }
</script>
</body><html>
 ベストアンサー
ベストアンサー
javascript
1const btns = document.querySelectorAll(".btn");
2btns.forEach((x,y)=>{
3  x.addEventListener('click',()=>{
4    console.log(y);
5  });
6});
15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる
 
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/08 02:54