Working with Events in JavaScript

These are useful and common patterns for working with events in JavaScript.

Events communicate data across components. Events can contain attributes with values set before the event is fired and read when the event is handled.

Fire a component event or an application event that’s registered on a component.

For more information, see:

To get the name of the event that’s fired:

To get an attribute that’s passed into an event:

To get all attributes that are passed into an event:

event.getParams() returns an object containing all event parameters.

To get the current phase of an event:

If the event hasn’t been fired, event.getPhase() returns undefined. Possible return values for component and application events are capture, bubble, and default. Value events return default. For more information, see:

To get the component that fired the event:

To retrieve an attribute on the component that fired the event:

To pause the fired event:

If paused, the event is not handled until event.resume() is called. You can pause an event in the capture or bubble phase only. For more information, see:

To cancel the default action on the event:

For example, you can prevent a lightning:button component from submitting a form when it’s clicked.

To resume event handling for a paused event:

You can resume a paused event in the capture or bubble phase only. For more information, see:

To set a value for an event parameter:

If the event has already been fired, setting a parameter value has no effect on the event.

To set values for parameters on an event:

If the event has already been fired, setting the parameter values has no effect on the event.

To prevent further propagation of an event:

You can stop event propagation in the capture or bubble phase only.