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

Assigning an Event Handler in HTML

function clickHandler(e){   $("div").html("clicked at X postion: " + e.screenX); } . . . <div onclick="clickHandler(event)">Click Here</div>

The most basic method of adding an event handler to a DOM element is directly in the HTML code. The advantage of this method is that it is simple and easy to see what event handler is assigned to a particular DOM object.

Event handlers are added to DOM objects in HTML by setting the value of the handler attribute in the tag statement. For each event that the element supports, there is an attribute that starts with on followed by the name of the event. For example, the click event attribute is onclick, and the load event attribute is onload ...

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