0

I'm new to JS, currently learning it and need your help. Below is just a sample of some code I came up with, and my question is: Which OF THE FOLLOWING is a valid statement for assigning the JS click events?

a. mButtons.forEach(e => e.click = process);
b. mButtons.forEach(e => e.onAction = process);
c. mButtons.forEach(e => e.onclick = process);
d. mButtons.forEach(e => e.onProcess = process);
e. None of the ABOVE
<body>

      <button>1</button>
      <button>2</button>
      <button>3</button>

      <script>
            let buttons = documnet.querySelectorAll("button");
            let mButtons = Array.from(buttons);

            const process = function () {
                  console.log({
                        question: 17,
                        date: new Date(),
                        label: this.innerText
                  });
            }
      </script>
</body>  

Ben96
  • 521
  • 1
  • 5
  • 18

1 Answers1

0

The answer is c. You have to bind the onclick

<body>

<button>1</button>
<button>2</button>
<button>3</button>

<script>

  let buttons = document.querySelectorAll("button");
  let mButtons = Array.from(buttons);

  const process = function(){
      console.log({question: 17, date: new Date(), label: this.innerText});
  }
  mButtons.forEach(e => e.onclick = process);

</script>
</body>
Sunil Lama
  • 4,531
  • 1
  • 18
  • 46