@studiometa/eventsdeprecated

A small utility to add and remove event listeners

Usage no npm install needed!

<script type="module">
  import studiometaEvents from 'https://cdn.skypack.dev/@studiometa/events';
</script>

README

Events

NPM Version Dependency Status devDependency Status

Three small functions to ease events binding in Javascript.

Installation

Install the package with your favorite package manager:

yarn add @studiometa/events
# or 
npm install @studiometa/events

Usage

Import the package in your project and use the on, once and off functions to bind/unbind your events.

import { on, off } from '@studiometa/events';

const links = document.querySelectorAll('a');
const preventDefault = e => e.preventDefault();

on(links, 'click', preventDefault);
off(links, 'click', preventDefault);

Documentation

on(target, type, handler, options)

Bind the given handler to the type event on the target.

Argument Type Description
target HTMLElement, NodeList, String The target of the event, can be a selector, an element or a list of elements
type String The type of event
handler Function The handler function
options Object Options for the addEventListener (documentation)

once(target, type, handler, options)

Bind the given handler to one and only one type event on the target.

Argument Type Description
target HTMLElement, NodeList, String The target of the event, can be a selector, an element or a list of elements
type String The type of event
handler Function The handler function
options Object Options for the addEventListener (documentation)

off(target, type, handler)

Unbind the given handler for the type event on the given target.

Argument Type Description
target HTMLElement, NodeList, String The target of the event, can be a selector, an element or a list of elements
type String The type of event
handler Function The handler function