JS Quiz: Easy: Simplest DOM event listener

JS Quiz answer: Option 2 (B).

Correct choice: myButton.addEventListener(‘click’, handleClick);

Why:
The standard and modern way to attach an event listener to a DOM element in JavaScript is using addEventListener(). It allows multiple listeners for the same event type on a single element. Options 1 and 3 are non-standard methods, and Option 4 (attachEvent) is an older, Internet Explorer-specific method.

Go deeper:

Using JavaScript Prototype To Add New Properties

First-answer leaderboard

  1. @emmawalter5 - 11 (firsts) :trophy:
  2. @kirupa - 8 (firsts)
  3. @Apexcodes - 6 (firsts)
  4. @adnanahmed - 2 (firsts)