How can I stop a default event when using a named function in addEvent?

Posted by Rupert on Stack Overflow See other posts from Stack Overflow or by Rupert
Published on 2010-04-15T16:42:06Z Indexed on 2010/04/15 17:03 UTC
Read the original article Hit count: 257

Filed under:
|

Normally, if I wish to stop a default event in mootools I can do this:

$('form').addEvent('submit', function(e) {
    e.stop();

    //Do stuff here
    }

However, I don't like using an anonymous function in events because I often want to reuse the code. Lets say I have a validate function. I could do this:

$('form').addEvent('submit', validate);

which works fine until I want to stop the default event. validate obviously doesn't know what e is so I can't just do e.stop(). Also I've tried passing the event as a variable to validate but whenever I use a named function with parameters, the function gets called automatically on domready, rather than on the event firing. Even worse, an error is thrown when the event is fired:

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about mootools