Skip to content

Instantly share code, notes, and snippets.

@tehsis
Created March 13, 2012 04:52
Show Gist options
  • Save tehsis/2026817 to your computer and use it in GitHub Desktop.
Save tehsis/2026817 to your computer and use it in GitHub Desktop.
jQuery plugin which gets the dataset() object of an element even on older browsers.
jQuery.fn.extend({
dataset: function(){
var attributes = {};
if(!this.length)
return {};
var elem = this[0];
var attrLength = elem.attributes.length;
if (elem.dataset) {
return elem.dataset;
}
// For no HTML5 browsers
for (var i = 0; i < attrLength; i++) {
var attr = elem.attributes[i];
if(!attr.specified){
//If IE specified == false
//If any other specified == undefined
continue;
}
else{
if ((/^data-/).test(attr.name)) {
attributes[attr.name.replace(/^data-/, "")] = attr.value;
}
}
}
return attributes;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment