Created
March 13, 2012 04:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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