addEventListener
can add multiple events, whereas with onclick
this cannot be done.onclick
can be added as an HTML
attribute, whereas an addEventListener
can only be added within <script>
elements.addEventListener
can take a third argument which can stop the event propagation.Both can be used to handle events. However, addEventListener
should be the preferred choice since it can do everything onclick
does and more. Don't use inline onclick
as HTML attributes as this mixes up the javascript and the HTML which is a bad practice. It makes the code less maintainable.