Question

I am using Mootools and adding a click event to a link. I have added a function to an event with this:

$('addCallRoute').addEvent('click', addCallRoute); // Add button

The function contains this:

function addCallRoute(e) {
    console.log(e);
}

The function that fires the event (without an actual click)

$('addCallRoute').fireEvent('click');

The Problem: When I click on the link physically, e is defined. but when I programmatically fire the event, e is undefined. Why?

Was it helpful?

Solution

Because you're not actually/physically triggering an action but firing it remotely. This is how it works.

event normally contains all sort of information about the element from which the action was triggered.

Always check if event is defined before trying to use any methods on it. Or do this:

link.fireEvent('click', {
    stop: function(){}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top