JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 823
  • Score
    100M100P100Q92478F
  • License MIT

Lightweight event delegation

Package Exports

  • @skax/delegate
  • @skax/delegate/dist/delegate.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@skax/delegate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@skax/delegate

Lightweight event delegation.

Install

You can get it on npm.

npm install @skax/delegate --save

Setup

Node (Browserify)
var delegate = require("@skax/delegate");
Browser (Standalone)
<script src="dist/delegate.js"></script>

Usage

Add event delegation

With the default base (document)

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

With an element as base

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

With a selector (of existing elements) as base

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

With an array/array-like of elements as base

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

Remove event delegation

With a single base element (default or specified)

const 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.

const delegations = delegate(
    ".container",
    ".btn",
    "click",
    function (e) {
        console.log(e.delegateTarget);
    },
    false,
);

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

Browser Support

Chrome logo Edge logo Firefox logo Internet Explorer logo Opera logo Safari logo
Latest ✔ Latest ✔ Latest ✔ 9+ ✔ Latest ✔ Latest ✔