» tagged pages
» logout
JavaScript
Return to JavaScript

Event Handling

(or Cancel)

(Editing anonymously: to be credited for your changes, login or register a new account)

other page actions:

Tags Applied to this Topic

1 person has tagged this page:

Javascript supports events, when they are triggered, callback functions may be triggered.

A basic way of attaching an event simply by defining one of the ‘on*’ event properties on a dom object to a callback function.


myinput.onclick = function() { alert('hi'); };

//or

myinput.onclick = callbackfunction;

function callbackfunction() { alert('hi'); }

This will trigger an alert on clicking ‘myinput’.

Within callback functions, you might want to access details of the event that triggered the callback, such as the source element.

For standards compliant browsers, such as Firefox, you can set your callback to have an argument, which will be populated with the event object that triggered the callback. For IE, you should test if this argument is null, and if it is, set it to ‘window.event’. In IE, you can also use a proprietary function called attachEvent() to add events to objects, in this case the first callback argument will be populated with the event as normal.

Additionally in a callback function, you might be interested in looking at the object that triggered the event. In Firefox and other standards compliant browsers, the ‘this’ variable is populated with the object that triggered the event. Again, this is not the case in IE, instead the source element is referenced through event.srcElement.

Username:
Password:
(or Cancel)