The 2.5 Ways to Handle Web Events

Chris Coyier Chris Coyier on

How many ways are there to add an event (like a click) to an element? Andrea Giammarchi covers them.

  1. <tag onclick />
  2. tag.onclick = listener
  3. tag.addEventListener('click', listener[, options])

The first two are basically the same (hence the “2.5” in the title). I’d say the vast majority of event handling is through addEventListener, but the clearly listed advantages/disadvantages Andrea goes through are worth understanding. The declarative nature of the first option can be quite handy.

It's time to take your JavaScript to the next level

Frontend Masters logo

Frontend Masters is the best place on the web to really learn JavaScript. We have a complete learning path from the biggest and best teachers in JavaScript to help you make the most out of the web's biggest language.

7-Day Free Trial

Leave a Reply

Your email address will not be published. Required fields are marked *

Did you know?

Frontend Masters Donates to open source projects. $363,806 contributed to date.