# 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](/docs/platform/aura-platform/guide/js-cb-dynamic-handler.md)
- [removeEventHandler()](/docs/platform/aura-platform/guide/ref-jsapi-component-removeeventhandler.md)
