From f6729d374689d2e3d161cfd8d133ef16a6f41fe1 Mon Sep 17 00:00:00 2001 From: vlw Date: Wed, 4 Dec 2024 08:45:50 +0000 Subject: [PATCH] doc: add "neat stuff" example to README --- README.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 363a0d2..961dcf5 100644 --- a/README.md +++ b/README.md @@ -12,31 +12,42 @@ new Elevent("click", document.querySelector("#myButton"), () => console.log("But ## Bind from a NodeList ```js -new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent +new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent ``` ## Bind with a CSS selector ```js // Bind all elements that match the CSS selector string -new Elevent("click", ".css-selector", (event, elevent) => console.log(event, elevent)) // console: PointerEvent, Elevent +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 ```js -const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance +const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance -elevent.remove(document.querySelector("#specialButton")); // Remove a specific HTMLElement if bound +elevent.remove(document.querySelector("#specialButton")); // Remove a specific HTMLElement if bound elevent.remove(); // Remove ALL currently bound elements ``` ## Bind a specific element ```js -const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance +const elevent = new Elevent("click", null, () => {}); // Empty Elevent instance elevent.bind(document.querySelector("#bindMe")); ``` +# Neat stuff + +## Create a [`once`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#once) event +```js +// As a variable +const elevent = new Elevent("click", null, () => elevent.remove()); + +// Anonymous function second argument +new Elevent("click", null, (event, elevent) => elevent.remove()); // PointerEvent, Elevent +``` + # Description ### `Elevent.constructor`