addEvent( obj, eventName, func )



This function attaches an event to an object. For example, you can attach a “click” event to a button. The same can be accomplished by assigning the onclick property on a button; however, with the addEvent() function, you can attach multiple unrelated “click” events to the same object.

Parameters:

  • obj – the object or element to which the event will be applied

  • eventName – the event name. Example: “click”, “mouseover”, etc.

  • func – the event handler function

Examples:

The following code assigns an event to a button.

// get the button object var myButton = getObj("my_button"); // assign the greeting() function as an event handler addEvent(myButton, "click", greeting); function greeting() { alert("Hello There!"); }

Alternatively, an anonymous function can be used when assigning the event.

// get the button object var myButton = getObj("my_button"); // assign anonymous function as an event handler addEvent(myButton, "click", function () { alert("Hello There!"); });