0

Code:

var Buttons = document.getElementsByClassName("Buttons");

var Draw = function (Index) {
  console.log(i);
  Buttons[Index].style.backgroundColor = "rgb(0, 0, 0)";
}

for (var i = 0; i < Buttons.length; i++) {
  Buttons[i].addEventListener("click", function (e) {
    Draw(i);
  });
}
<!DOCTYPE html>
<html lang="en">
  <body>
    <button type="button" class="Buttons">0</button>
    <button type="button" class="Buttons">1</button>
    <button type="button" class="Buttons">2</button>
    <button type="button" class="Buttons">3</button>
    <button type="button" class="Buttons">4</button>
  </body>
</html>

I wanted each button to put in a different number, but they all give 5 to the Index parameter.

Why.

Falling10fruit
  • 197
  • 1
  • 12

0 Answers0