# delegate

> Lightweight event delegation

Latest version **3.2.0** (published 2017-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install delegate
pnpm add delegate
yarn add delegate
bun add delegate
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2017-12-05 |
| First published | 2012-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 186 |
| Maintainers | zenorocha |
| Keywords | event, delegate, delegation |

## Links

- npm: https://www.npmjs.com/package/delegate
- Repository: https://github.com/zenorocha/delegate
- Homepage: https://github.com/zenorocha/delegate#readme
- Issues: https://github.com/zenorocha/delegate/issues
- npm.io page: https://npm.io/package/delegate

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 3.2.0 (latest) — 2017-12-05
- 3.1.3 — 2017-05-29
- 3.1.2 — 2017-02-28
- 3.1.1 — 2016-12-12
- 3.1.0 — 2016-10-23
- 3.0.2 — 2016-09-25
- 3.0.1 — 2016-01-12
- 3.0.0 — 2015-11-13
- 2.0.3 — 2015-11-13
- 2.0.2 — 2015-10-26
- 2.0.1 — 2015-10-26
- 2.0.0 — 2015-10-26
- 1.0.0 — 2015-10-20
- 0.0.1 — 2012-07-23

## README

# delegate

Lightweight event delegation.

## Install

You can get it on npm.

```
npm install delegate --save
```

If you're not into package management, just [download a ZIP](https://github.com/zenorocha/delegate/archive/master.zip) file.

## Setup

###### Node (Browserify)

```js
var delegate = require('delegate');
```

###### Browser (Standalone)

```html
<script src="dist/delegate.js"></script>
```

## Usage

### Add event delegation

#### With the default base (`document`)

```js
delegate('.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);
```

#### With an element as base

```js
delegate(document.body, '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);
```

#### With a selector (of existing elements) as base

```js
delegate('.container', '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);
```

#### With an array/array-like of elements as base

```js
delegate(document.querySelectorAll('.container'), '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);
```

### Remove event delegation

#### With a single base element (default or specified)

```js
var delegation = delegate(document.body, '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

delegation.destroy();
```

#### With multiple elements (via selector or array)

Note: selectors are always treated as multiple elements, even if one or none are matched. `delegate()` will return an array.

```js
var delegations = delegate('.container', '.btn', 'click', function(e) {
    console.log(e.delegateTarget);
}, false);

delegations.forEach(function (delegation) {
    delegation.destroy();
});
```

## Browser Support

| <img src="https://clipboardjs.com/assets/images/chrome.png" width="48px" height="48px" alt="Chrome logo"> | <img src="https://clipboardjs.com/assets/images/edge.png" width="48px" height="48px" alt="Edge logo"> | <img src="https://clipboardjs.com/assets/images/firefox.png" width="48px" height="48px" alt="Firefox logo"> | <img src="https://clipboardjs.com/assets/images/ie.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="https://clipboardjs.com/assets/images/opera.png" width="48px" height="48px" alt="Opera logo"> | <img src="https://clipboardjs.com/assets/images/safari.png" width="48px" height="48px" alt="Safari logo"> |
|:---:|:---:|:---:|:---:|:---:|:---:|
| Latest ✔ | Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | Latest ✔ |

## License

[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha

---
_Source: https://npm.io/package/delegate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
