Skip to content

Instantly share code, notes, and snippets.

@remy
Created January 22, 2010 18:24
Show Gist options
  • Save remy/284002 to your computer and use it in GitHub Desktop.
Save remy/284002 to your computer and use it in GitHub Desktop.
(function () {
function detectMutation() {
mutationSupported = true;
this.removeEventListener('DOMAttrModified', detectMutation, false);
}
var forEach = [].forEach,
regex = /^data-(.+)/,
el = document.createElement('div'),
mutationSupported = false;
el.addEventListener('DOMAttrModified', detectMutation, false);
el.setAttribute('foo', 'bar');
Element.prototype._datalistCache = {};
Element.prototype._datalistCacheValid = false;
Element.prototype.__defineGetter__('datalist', function () {
var i = this.attributes.length, match, el = this;
if (!mutationSupported || !this._datalistCacheValid) {
this._datalistCacheValid = true;
forEach.call(this.attributes, function(attr) {
if (match = attr.name.match(regex))
el._datalistCache[match[1]] = attr.value;
});
}
return this._datalistCache;
});
document.body.addEventListener('DOMAttrModified', function (event) {
event.target._datalistCacheValid = false;
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment