Why should you use event listeners instead of on<event>?

Because Qwerty loves callback hell

Both have specific use cases. If you need something quick and easy, onclick should do the trick, but if you need to register multiple events, addEventListener is what you’ll end up using. I’d personally rather use event listeners but for something as simple as alerting onclick should be suitable.

(If you’re gonna use event listeners always use ES6 tho or you’re gonna make a lot of people mad)

4 Likes

Because MattMattMatt said so

cc: @MattDESTROYER pls explain why ty


you’re*

3 Likes

EventListeners is better for code maintainability and readability (besides being a good practice).
By using addEventListener , you’re separating your HTML (structure) from your JavaScript (behavior).

And as you said, you can have multiple listeners. With onclick, everytime you would assigning a new onclick function, the function would overwrite any existing ones, but with addEventListener you can add as many event handlers as you need.

If his project scale to something further, having used EventListeners will pay off too.

7 Likes