Can I make getElementById reactive?
I want to use document.getElementById () as signal.
Is it possible?
4 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Should I update the signal manaully byself whenever the element is removed or added?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
The only problem is: how do you know when it changes?
- Use MutationObserver on the document, observe all children, check if the signal needs to be updated in the MutationObserver callback
- make a requestAnimationFrame loop (ugh) and check the result every frame, then set the signal
The rAF way is probably easier, but MutationObserver is snappier and not running endlessly.
This only works on
document
. If you are in a ShadowRoot, you have to call shadowRoot.getElementById