Languages
[Edit]
EN

JavaScript - differences between onclick and addEventListener

0 points
Created by:
Laylah-Walsh
624

In this article, we would like to show you the differences between onclick and addEventListener in JavaScript.

 

onclick

  • with onclick you can't attach multiple events to the same element,

  • you can't add event handlers from different places in your code because you can only attach one event handler,

  • someone else may overwrite your event elsewhere in the code,

addEventListener

  • With addEventListener you can attach multiple events to the same element,
  • you can add event handling functions from different places in your code even for the same event,
  • the event can't be overwritten elsewhere in the code (new event handler will be added to the same event),
  • if you want to remove events that were added using addEventListener you must always have a handle to the function that was added with the addEventListener.

Alternative titles

  1. JavaScript - addEventListener vs onclick
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join