O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Event Handlers in JavaScript

function clickHandler(e, data){   (Event Handler Code) } ... document.getElementById("div1").addEventListener('click',   function(e){     eventHandler(e,"data");   },false);

You can also dynamically add and remove event handlers to DOM objects inside JavaScript code. To add an event handler in JavaScript, simply call addEventListener() on the DOM object.

The addEventListener() function takes three parameters. The first is the event type (event types are defined in Table 6.1), a function to call and a boolean that specifies true if the handler should be called during the capturing phase and the bubbling phase or false if the handler should be called only during the bubbling ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required