Socket
Socket
Sign inDemoInstall

delegate

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    delegate

Lightweight event delegation


Version published
Weekly downloads
1.3M
decreased by-13.93%
Maintainers
1
Install size
18.0 kB
Created
Weekly downloads
 

Package description

What is delegate?

The 'delegate' npm package is designed to provide a straightforward and efficient way to manage and delegate event handlers in JavaScript. It allows developers to attach a single event listener to a parent element that can handle events from child elements, matching a specific selector. This approach is particularly useful for handling events on dynamically added elements or for reducing the number of event listeners in an application, thereby improving performance.

What are delegate's main functionalities?

Event delegation

This feature allows you to attach an event listener to a parent element (in this case, a 'ul' with id 'myList') that listens for clicks on its child 'li' elements. When a 'li' element is clicked, the provided function is executed. This is useful for handling events on elements that may not exist at the time the script runs or for minimizing the number of event listeners in your application.

document.delegate('ul#myList', 'click', 'li', function(event) {
  console.log('List item clicked:', this);
});

Other packages similar to delegate

Readme

Source

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

Setup

Node (Browserify)
var delegate = require('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)
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.

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

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

Browser Support

Chrome logoEdge logoFirefox logoInternet Explorer logoOpera logoSafari logo
Latest ✔Latest ✔Latest ✔9+ ✔Latest ✔Latest ✔

License

MIT License © Zeno Rocha

Keywords

FAQs

Last updated on 05 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc