JavaScript · Junior · Question 40 of 75

Why is addEventListener better than el.onclick = handler?

Explanation

Reassigning onclick overwrites the previous handler. addEventListener adds them independently and supports the capture phase (capture), one-time handlers and removal via signal (AbortController). Remove handlers with removeEventListener and the same function.

Further reading

Problem with a question

JavaScript · Junior · question 40

Test yourself

Take the JavaScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz