# vent
**Repository Path**: mirrors_adobe/vent
## Basic Information
- **Project Name**: vent
- **Description**: DOM event delegation that actually works
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-09-24
- **Last Updated**: 2026-03-07
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Vent
> DOM event delegation that actually works
Vent is a well-tested event delegation library that supports real DOM events, capture phase listeners, namespaces, and scoped selectors.
## What is event delegation?
*Event delegation* is a pattern that takes advantage of [event propagation](https://github.com/adobe/vent#capture-vs-bubbling) to let you easily handle events originating from specific descendant elements. With event delegation and the power of CSS selectors, you can handle events originating from any number of elements or add event listeners before the elements you want to listen to are even added to the DOM.
Vent implements the event delegation pattern with a simple, powerful, and familiar API.
```js
var vent = new Vent(document.body);
// Call the handler when any element with the sayHi CSS class is clicked
vent.on('click', '.sayHi', function handler() {
console.log('Hello world!');
});
```
## Why Vent?
There are other event delegation libraries out there, so here's how Vent is different:
* **NS**: Supports event namespaces, i.e. `click.myApp`
* **Scoped**: Supports scoped selectors, i.e. `> .immediateChild`
* **Real DOM**: Dispatches real, bubbling DOM events
* **Capture**: Supports listening to events during the capture phase
* **Tests**: Has comprehensive tests
Name | NS? | Scoped? | Real DOM? | Capture? | Tests?
----------------|:-------------------|:------------------:|:-----------------:|:-----------------:|:-------------------:
[Vent] | :white_check_mark: | :white_check_mark: | :white_check_mark:| :white_check_mark:| :white_check_mark:
[jQuery] | :white_check_mark: | :white_check_mark: | :x: | :x: | :white_check_mark:
[Gator] | :x: | :x: | :white_check_mark:| :x: | :x:
[ftdomdelegate] | :x: | :x: | :white_check_mark:| :white_check_mark:| :white_check_mark:
[Vent]: #vent
[jQuery]: https://github.com/jquery/jquery
[Gator]: https://github.com/ccampbell/gator
[ftdomdelegate]: https://github.com/ftlabs/ftdomdelegate
## API
### new Vent(elementOrSelector) -> vent
Create a new Vent instance with the root element as the provided element or selector.
Events will be listened to for the root element and its current or future children.
#### vent.on(eventName[, selector], handler[, useCapture]) → this
Add an event listener.
##### Parameters
Name | Type | Required | Default | Description
-------------|----------|----------| ---------|------------
`eventName` | String | **yes** | - | The event name to listen for, including optional namespace(s).
`selector` | String | no | - | The selector to use for event delegation.
`handler` | Function | **yes** | - | The function that will be called when the event is fired.
`useCapture` | Boolean | no | false † | Only remove listeners with useCapture set to the value passed in.
† For [`focus`](https://developer.mozilla.org/en-US/docs/Web/Events/focus) and [`blur`](https://developer.mozilla.org/en-US/docs/Web/Events/blur) events, `useCapture` will default to `true` unless explcitly specified as these events do not bubble.
#### vent.off(eventName[, selector], handler[, useCapture]) → this
Remove an event listener.
##### Parameters
Name | Type | Required | Description
-------------|----------|----------|------------
`eventName` | String | no | Only remove listeners for the provided event name and/or namespace(s).
`selector` | String | no | Only remove listeners that have this selector.
`handler` | Function | no | Only remove listeners that have this handler.
`useCapture` | Boolean | no | Only remove listeners that are captured.
#### vent.dispatch(eventName[, options]) → CustomEvent
Dispatch a custom event at the root element.
##### Parameters
Name | Type | Required | Default | Description
----------------------|----------|----------| ---------|------------
`eventName` | String | **yes** | - | The name of the event to dispatch.
`options` | Object | no | - | CustomEvent options.
`options.bubbles` | Boolean | no | true | Whether the event should bubble.
`options.cancelable` | Boolean | no | true | Whether the event should be cancelable.
`options.detail` | * | no | - | Data to pass to listeners as `event.detail`.
## Examples
### Create a Vent instance
You can pass anything that implements the `EventTarget` interface:
```js
var vent = new Vent(window);
var vent = new Vent(document.body);
var vent = new Vent(document.documentElement);
```
Including HTML elements:
```js
var div = document.createElement('div');
var vent = new Vent(div);
```
You can also pass a selector:
```js
var vent = new Vent('#myApp');
```
### Adding direct event listeners
Listen to an event directly on the element, including those that bubble from descendant elements:
```js
vent.on('resize', function(event) {
console.log('Window resized!');
});
```
### Adding delegated event listeners
Listen to an event on child elements that match the provided selector:
```js
vent.on('click', '.reset', function(event) {
console.log('Should reset!');
});
```
The child element does not have to be in the DOM at the time the listener is added.
### Removing listeners
Remove all listeners added with this Vent instance:
```js
vent.off();
```
Remove all listeners for click events:
```js
vent.off('click');
```
Remove all listeners on the .reset selector:
```js
vent.off(null, '.reset');
```
Remove all listeners that call the provided handler:
```js
vent.off(null, null, handler);
```
Remove all listeners that listen during the capture phase:
```js
vent.off(null, null, null, true);
```
Remove all listeners for click events that call the provided handler:
```js
vent.off('click', null, handler);
```
Remove all listeners that match all criteria:
```js
vent.off('click', '.reset', handler, true);
``````
### Event properties and handler context
Vent sets properties of the event object and context of the handlers as follows:
* `this` - The element that matched for delegation
* `event.matchedTarget` - The element that matched for delegation (same as `this`)
* `event.currentTarget` - The root element of the Vent instance
* `event.target` - The element that originally dispatched the event
Assuming the following HTML structure:
```html