Posts Photos Archives About

Say I add events to an object using either addEventListener or attachEvent (depending on the browser); is it possible to later invoke those events programmatically?

The events handlers are added/removed using an object like this:

var Event = {
    add: function(obj,type,fn) {
        if (obj.attachEvent) {
        } else {
    remove: function(obj,type,fn) {
        if (obj.detachEvent) {
        } else {

Or do I need to store copies of each handler and just add an Event.invoke(...) function?

Edit: Also, jQuery is not an option :D


Mark Redman said...

Can you not create functions that do the work required, run those from the events then run those same functions later when required?

NickFitz said...

As usual, you have to do it one way for Internet Explorer, and the correct way for everything else ;-)

For IE:


See the MSDN library for more info.

For the real browsers:

var event = document.createEvent("MouseEvent");
event.initMouseEvent("mouseover", true, true, window);

Note that, although the second, standards-based approach seems more long-winded, it is also considerably more flexible: check the documentation, starting with the Mozilla DOM Event Reference at

Although only tangentially related to what you're trying to do (it's related to custom events, rather than normal ones) Dean Edwards has some example code at that may be worth a look.