Which method is used to attach an event handler to an element in the DOM?
Answer
addEventListener
Answer
addEventListener
The DOM method used to attach an event handler to an element is addEventListener.
It belongs to the modern DOM event model and lets a script register a function for events such as clicks, keyboard input, pointer movement, or form submission. A typical call is `button.addEventListener("click", handleClick)`, where the first argument names the event and the second supplies the callback.
A common mix-up is `onclick`. Assigning `element.onclick = handler` is also a valid older-style technique, but `onclick` is an event property rather than the general-purpose method normally meant by this question. It allows only one assigned handler at a time, while addEventListener can register multiple listeners for the same event. The historical `attachEvent` method was an Internet Explorer-specific alternative and is obsolete.
The DOM Level 2 Events model introduced addEventListener along with related methods such as removeEventListener and dispatchEvent. Its options also support event capture, bubbling behavior, and one-time listeners, making it the standard choice for maintainable browser code.
Source: Wikipedia · fact-checked Aug. 2026