1.0.4 • Published 7 years ago
@studiometa/events v1.0.4
Events
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/eventsUsage
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 |