Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This function attaches an event to an object. For example, you can attach an a “click” event to a button. The same can be accomplished by assigning the onlclick 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.

Code Block
javascript
javascript

// 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!");
}

...

Code Block
javascript
javascript

// get the button object
var myButton = getObj("my_button");

// assign anonymous function as an event handler
addEvent(myButton, "click", function ()
{
     alert("Hello There!");
});