JSFiddle - React, Tailwind, and code Playground
HTML
<input id="inputEl" placeholder="Update Value here..." type="text" />
<span id="val"></span>
JavaScript
const data = {
value: 'Hello World'
};
Object.defineProperty(data, 'prop', {
get: function() {
console.log('Getter called');
return this.value;
},
set: function(value) {
console.log('Setter called');
this.value = value;
printVal();
}
});
const el = document.getElementById('inputEl');
el.addEventListener('keyup', (event) => {
data.prop = event.target.value;
});
function printVal() {
const el = document.getElementById('val');
el.innerText = data.prop;
}