Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Created September 28, 2016 05:33
Show Gist options
  • Save joshuacerbito/84440729ef4a89dfb88b9eff78c509a4 to your computer and use it in GitHub Desktop.
Save joshuacerbito/84440729ef4a89dfb88b9eff78c509a4 to your computer and use it in GitHub Desktop.
IE-compatible data attribute getter/setter
Object.prototype.data = function (prop, val) {
var _return,
notIE = ( this.dataset !== undefined );
return ( notIE )? // not IE
( typeof val === "undefined" ) ? // get data value
this.dataset[prop] : this.dataset[prop] = val
: // is IE
( typeof val === "undefined" ) ? // get data value
this.getAttribute('data-' + prop) : this.setAttribute('data-' + prop, val);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment