Skip to content

Instantly share code, notes, and snippets.

@frentsel
Last active February 10, 2017 13:03
Show Gist options
  • Save frentsel/c902fcf4be2a99487e74 to your computer and use it in GitHub Desktop.
Save frentsel/c902fcf4be2a99487e74 to your computer and use it in GitHub Desktop.
Simple two-way data-binding
var Field = function(selector){
var el = document.querySelector(selector),
callback;
this.get = function() {
return el.value;
};
this.set = function(val) {
el.value = val;
};
this.handler = function(_callback) {
callback = _callback;
};
el.onchange = el.oninput = el.onpaste = function(){
callback(el.value);
};
};
var input = new Field("input");
input.set("Sanya");
input.handler(console.log);
input.get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment