November 2012
Intermediate to advanced
106 pages
2h 20m
English
Since the dawn of JavaScript, browsers have allowed event handlers to be attached to DOM elements like so:
| | link.onclick = clickHandler; |
Ah, simplicity itself! There’s just one caveat: if you wanted two click handlers for an element, you’d have to aggregate them yourself with a wrapper function.
| | link.onclick = function() { |
| | clickHandler1.apply(this, arguments); |
| | clickHandler2.apply(this, arguments); |
| | }; |
Not only is this tedious, it’s also a recipe for bloated, all-purpose handler functions. That’s why the W3C added addEventListener to the DOM specification in 2000 and jQuery abstracted it with the bind method. bind makes it easy to add as many handlers as you like to any event on any element (or set of elements), without worrying ...