Attach and detach callback functions from events on HTML elements
Find a file
Victor Westerlund 68116c3b09 Replace HTMLCollection with NodeList (#1)
I had some weird notion that `querySelectorAll` would return an `HTMLCollection` and not a `NodeList`.

```js
[...document.querySelectorAll()].forEach(element => {});
```
> Maybe I was doing this for no reason before, since `NodeList` already inherits the Array `forEach()` method...

Reviewed-on: https://codeberg.org/vlw/elevent/pulls/1
Co-authored-by: Victor Westerlund <victor.vesterlund@gmail.com>
Co-committed-by: Victor Westerlund <victor.vesterlund@gmail.com>
2024-09-13 11:49:15 +00:00
src Replace HTMLCollection with NodeList (#1) 2024-09-13 11:49:15 +00:00
.gitignore Initial code commit 2024-09-11 16:49:29 +02:00
.npmignore Initial code commit 2024-09-11 16:49:29 +02:00
LICENSE Initial commit 2024-09-11 12:25:18 +00:00
package.json fix: npm package version 2024-09-13 10:45:08 +02:00
README.md Replace HTMLCollection with NodeList (#1) 2024-09-13 11:49:15 +00:00

Elevent

Create callback functions for any EventTarget dispatched on HTML elements.

import { Elevent } from "https://cdn.jsdelivr.net/npm/elevent/src/Elevent.mjs"

new Elevent("click", document.querySelector("#myButton"), () => console.log("Button clicked!")); // console: "Button clicked"

Examples

Bind from a NodeList

new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent

Bind with a CSS selector

// Bind all elements that match the CSS selector string
new Elevent("click", ".css-selector", (event, elevent) => console.log(event, elevent)) // console: PointerEvent, Elevent

New elements added to the DOM with a matching CSS selector will bind automatically

Remove event listeners

const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance

elevent.remove(document.querySelector("#specialButton")); // Remove a specific HTMLElement if bound
elevent.remove(); // Remove ALL currently bound elements

Bind a specific element

const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance

elevent.bind(document.querySelector("#bindMe"));

Description

Elevent.constructor

Elevent.constructor(
    eventType: string | null
    target: NodeList | HTMLElement | string | null,
    callback: CallableFunction
)

this.bind()

this.bind(
    target: HTMLElement
)

this.remove()

this.remove(
    target?: HTMLElement
)