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

Leave a Reply

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

$966,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.