Add support for HTML elements to listener binding

This commit is contained in:
Talia 2021-11-24 16:16:04 +01:00
parent 945736b9a9
commit 5036259d75
2 changed files with 7 additions and 2 deletions

View file

@ -45,7 +45,10 @@ export const bind = (listener, prop, target=document.createTextNode(""), filter)
? filter(data)
: data
if ("innerText" in target)
if (typeof data == "string")
target.innerText = data
else
target.replaceChildren(data)
else
target.data = data
}

View file

@ -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