How many ways are there to add an event (like a click
) to an element? Andrea Giammarchi covers them.
<tag onclick />
tag.onclick = listener
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.