Assuming I have the following:
<div id="parent">
<div id="child-one"><p>Child One</p></div>
<div id="child-two"><p>Child Two</p></div>
<div id="child-three"><p>Child Three</p></div>
</div>
As I understand it, hyperHTML sets the innerHTML of the the element that you bind to. If I want to not only change the innerHTML of #child-two but also change its attributes without touching #child-one and #child-three, what is the best way to accomplish this using hyperHTML?
Is there a way to bind directly to an element to be modified rather than its parent?
I know I could use wire to create a new element and then completely replace the existing element, but then I would not have the ability to continue to bind and update only what has changed.
There are few misconceptions here, and I'll try to address all of them.
hyperHTML is not innerHMTL
People see template literals, they instinctively assume it's
innerHTML, buthyperHTMLis everything butinnerHTML.The name is similar on purpose, but the implementation has nothing to do with strings, strings are just the declarative facade used to map "all the things" and glue the logic together.
Differences between the two:
innerHTMLtrashes every time every node and create everything from the scratch, whilehyperHTMLrelate always see nodes to a specific contextinnerHTMLalways requires a parent element to be injected,hyperHTMLhas thewirewhich is also what you are looking for, but I'll explain this laterinnerHTMLlet you define broken layouts,hyperHTMLwill throw up if you do something wrong and expect specific semantics (i.e. no partial attributes shenanigans)Apologies if you knew this already, but I think it's mandatory to clarify the very root concept behind
hyperHTML.Now, let's move on :-)
Yes, the wire.
No. Wires have ids, so that each ID will always return the exact same node.
If you want to wire
#child-twoto its parent, but you could also relate it globally, if needed, you can simply wire it through an id.You can test above code live in code pen.