Mozilla: “event is not defined”

Tagged: JavaScript Date: 24th, March 2008

One of most common event related questions I got is about error in Mozilla browsers “event is not defined”.
IE and Mozilla treat event variable differently, while in IE it is a global variable (accessible without passing it to the function) in Mozilla you need to pass it every time (which btw is by w3c standards).

Example 1:
this would work in IE, but NOT in Mozilla

element.onkeypress = function() {
return isNumberInput(this, event);
};

Example 2:
this works in IE AND Mozilla
notice that event variable is passed to the function

element.onkeypress = function(event) {
return isNumberInput(this, event);
};

2 Responses to “Mozilla: “event is not defined””

  1. ktjrdn:

    Thanks for such a simple answer!

  2. Jeff J. Snider:

    Thank you! You just helped me solve what turned out to be a simple problem.

Leave a Reply