DaBi (short for Data Binding) is a dead simple yet complete and self-contained DOM-to-JS and JS-to-DOM data binding library in just 25 lines of pure ES5 and 454 bytes when minified.
Download it right here or include it into your HTML:
<script src="//cdn.rawgit.com/plugnburn/f9d3acf33bee8f3f7e2d/raw/50d2148811f8da61d70cdceec83ecbde5e484b83/dabi.min.js"></script>
Unlike most similar libraries, DaBi operates neither on data-
attributes nor on single DOM elements. It operates on selectors, thus is able to robustly handle element lists that change over time.
You bind a property of an object to the selector with a single DaBi(selector, object, property[, isEnumerable = false])
call, and that's all you really need. For example:
var myContent = {}
DaBi('p.main', myContent, 'mainParagraph') // data-to-DOM binding example
DaBi('input.new', myContent, 'newContent', true) // DOM-to-data binding example with enumeration enabled
That's it. Whenever you modify myContent.mainParagraph
property, a <p>
element with main
class will be populated with its content. And whenever you input something into a text field with new
class, the myContent.newContent
JS property will be set accordingly to the first matching element value. In addition, you can enable enumerating the value with standard ways (for .. in
, Object.keys
etc) by setting the fourth argument, isEnumerable, to true
.
Actually, all bound DOM values are read on demand, so they will always reflect actual DOM data state.
Should run perfectly on any ES5-compliant browser.
Why not take advantage of .textContent instead of innerHTML ?