Skip to content

Instantly share code, notes, and snippets.

@Yuffster
Created July 25, 2012 05:48
Show Gist options
  • Save Yuffster/3174627 to your computer and use it in GitHub Desktop.
Save Yuffster/3174627 to your computer and use it in GitHub Desktop.
Data attribute API extension for MooTools.
Element.implement({
getData: function() {
var data = {}, attrs = this.attributes;
for (var i=0; i<attrs.length; i++){
k = attrs.item(i).nodeName;
if (k.match(/^data-/)) data[k.replace(/^data-/, '')] = attrs.item(i).nodeValue;
} return data;
},
clearData: function() {
var self = this;
Object.each(this.getData(), function(v,k){
self.removeProperty('data-'+k, '');
});
return this;
},
setData: function(k,v) {
this.setAttribute('data-'+k, v);
return this;
}
});
@assertchris
Copy link

Wouldn't it be grand to have an element wrapper which mimics the .data attributes in the spec, using timers or property change events? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment