
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Change detection mechanisms based on MutationObserver interface. It provides the ability to watch for changes being made to the DOM tree. You may connect node elements in One-Time, One-Way & Two-Way binding.
npm install bind-dom
yarn add bind-dom
import { oneTime, oneWay, twoWay, disconnect, disconnectAll } from 'bind-dom'
import bindDom from 'bind-dom'
import bindDom from 'bind-dom'
bindDom.oneWay('oneWayObserver', document.querySelector('#targetNode'), document.querySelector('#toNode'))
bindDom.disconnect('oneWayObserver')
import { twoWay, disconnect } from 'bind-dom'
twoWay('twoWayObserver', document.querySelector('#targetNode'), document.querySelector('#targetNode_2'))
disconnect('twoWayObserver')
MutationObserver is watching changes being made to the DOM tree. So, for input/textarea elements make sure that attributes are changed. See the examples below.
// JS
function changeValueAttr(event) {
event.target.setAttribute('value', event.target.value)
}
// HTML
<input type="text" onkeyup="changeValueAttr(event)" />
// JS
function changeAttr(event) {
event.target.setAttribute('data-bind-dom', event.target.value.length)
}
// HTML
<textarea data-bind-dom="" onkeyup="changeAttr(event)"></textarea>
oneTime(observerName, targetNode, toNode, config) - binding occurs one time when element content change
oneWay(observerName, targetNode, toNode, config) - creates an ongoing connection between targetNode and toNode
twoWay(observerName, targetNode, toNode, config) - creates a vice versa connection between targetNode and toNode
disconnect(observerName) - disconnects observer
disconnectAll() - disconnects all observers
{String} observerName - unique observer name
{Element} targetNode - node element which is going to be observe for DOM changes
{Element} toNode - node element to which changes is going to be applied (twoWay binding vice versa with targetNode)
{Object} config - specs
const config = {
// what target objects to observe
// { attributes: true, childList: true, characterData: true } by default
members: {
attributes: true,
childList: true,
subtree: true,
characterData: true,
attributeOldValue: true,
characterDataOldValue: true
},
callback: (mutationsList) => {
// mutationsList[Array] - list of nodes which were changed
}
}
Any contributions you make are greatly appreciated.
Please read the Contributions Guidelines before adding your own helper or improvement to code snippets, explanations, etc.
MIT © Vasyl Stokolosa
FAQs
Synchronization between two DOM elements (oneTime, oneWay, twoWay)
The npm package bind-dom receives a total of 13 weekly downloads. As such, bind-dom popularity was classified as not popular.
We found that bind-dom demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.