---
schema_version: "1"
id: a23f635726dc116b6003c3b8ceb6539ef9b92d976947e4fbdc7df9fcbe161ab6
title: addEventHandler()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/ref-jsapi/ref-jsapi-component-addeventhandler.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/ref-jsapi-component-addeventhandler.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 459d0982f51963020a828b35a571e0917779918535039ed28b64c45aadf4017b
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# addEventHandler()

Dynamically adds an event handler for a component or application event.

## Signature

`addEventHandler(String event, function handler, String phase, Boolean includeFacets)`

## Parameters

**_event_:** Type: `String`. The name of the event to handle. For a component event, set this argument to match the name attribute of the `aura:registerEvent` tag. For an application event, set this argument to match the event descriptor, `namespace:eventName`.

**_handler_:** Type: `function`. The handler for the event. There are two format options for this argument.

-   To use a controller action, use the format: `cmp.getReference("c.actionName")`.
-   To use an anonymous function, use the format: `function(auraEvent) { // handling logic here }`

**_phase_:** Type: `String`. Optional. The event bubbling phase for which to add the handler. The default value is `"bubble"`.

**_includeFacets_:** Type: `Boolean`. If `true`, attempts to catch events generated by components transcluded by facets; for example `v.body`.

## Sample Code

```javascript
// For component event, first param matches name attribute in <aura:registerEvent> tag
cmp.addEventHandler("compEvent", cmp.getReference("c.handleEvent"));

// For application event, first param is event descriptor, "c:appEvent"
cmp.addEventHandler("c:appEvent", cmp.getReference("c.handleAppEvent"));

// Anonymous function handler for component event
cmp.addEventHandler("compEvent", function(auraEvent) {
    // add handler logic here
    console.log("Handled the component event in anonymous function");
});
```

**See Also**  


-   [Dynamically Adding Event Handlers To a Component](https://developer.salesforce.com/docs/platform/aura-platform/js-cb/js-cb-dynamic-handler.md)
-   [removeEventHandler()](https://developer.salesforce.com/docs/platform/aura-platform/guide/ref-jsapi-component-removeeventhandler.md)

