Blog |

How to Fix ReferenceError: Event is Not Defined in JavaScript

How to Fix ReferenceError: Event is Not Defined in JavaScript
Table of Contents

The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope.

The ReferenceError: event is not defined usually occurs while using an event handler if the event parameter is either not declared or declared incorrectly. For example, if on an onclick event, the handler does not declare the event parameter, this error is thrown.

 

What Causes Javascript ReferenceError: Event is Not Defined

The Javascript ReferenceError is thrown when an attempt is made to reference a non-existing or out of scope variable. Not defining a variable or parameter before referencing it is one of the most common triggers for reference errors.

When using event handlers in Javascript, the handler expects a parameter to be declared for the event object. This parameter is usually named e or event. If this parameter is not declared in the handler, or declared incorrectly, and is used later on in the handler, the ReferenceError: event is not defined is thrown.

The Javascript Window object has a property called event, which is only supported by certain browsers. In case an event parameter is not declared explicitly in event handlers, this property might instead be used. Therefore, this error only occurs in browsers that do not support the Window.event property.

 

ReferenceError: Event is Not Defined Example

Here’s an example of a Javascript ReferenceError: event is not defined thrown when the event parameter is not declared in an event handler:

function myFunction() {
    console.log(event);
}

In the above example, the event parameter is not declared in the onclick event handler myFunction(). If this code is run in a browser that does not support the Window.event property, the ReferenceError: event is not defined is thrown.

 

How to Fix ReferenceError: Event is Not Defined

The Javascript ReferenceError: event is not defined only occurs in browsers where the Window.event property is not supported. This property is deprecated and should be avoided in code.

It is best practice to use the Event object passed into event handler functions instead. This can be achieved by declaring the event parameter in event handler functions.

The earlier example can be updated to fix the error by using the above approach:

function myFunction(event) {
    console.log(event);
}

Here, since the event parameter is declared in the event handler function myFunction(), the event object can be used normally and the ReferenceError: event is not defined is avoided.

 

Track, Analyze and Manage Errors with Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing JavaScript errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape