From 5036259d755a8c90f931b3bde06141c2aecfdffb Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 24 Nov 2021 16:16:04 +0100 Subject: [PATCH] Add support for HTML elements to listener binding --- listener.js | 5 ++++- listener.md | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/listener.js b/listener.js index fc381a3..24c4ed7 100644 --- a/listener.js +++ b/listener.js @@ -45,7 +45,10 @@ export const bind = (listener, prop, target=document.createTextNode(""), filter) ? filter(data) : data if ("innerText" in target) - target.innerText = data + if (typeof data == "string") + target.innerText = data + else + target.replaceChildren(data) else target.data = data } diff --git a/listener.md b/listener.md index f198ac0..3d31336 100644 --- a/listener.md +++ b/listener.md @@ -22,7 +22,9 @@ bind(listener, "value") bind(listener, "value", html_element) // Binds an existng HTML or Text node bind(listener, "value", html_element, value => value.toUpperCase()) -// Filters the value through a function before setting it +// Accepts a function to transform the value +bind(listener, "value", html_element, value => document.createElement("hr")) +// Also accepts HTML elements when the target is not a text node ``` ## Example