Skip to content

Instantly share code, notes, and snippets.

@ledbettj
Created June 20, 2012 19:51
Show Gist options
  • Select an option

  • Save ledbettj/2961819 to your computer and use it in GitHub Desktop.

Select an option

Save ledbettj/2961819 to your computer and use it in GitHub Desktop.
function nameToCamel(name) {
words = name.split(/[-_]/);
for(var i = 0; i < words.length; ++i) {
words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1);
}
return words.join('');
}
var node = document.getElementById("test");
var attrs = {};
for(var i = 0; i < node.attributes.length; ++i) {
var attr = node.attributes[i];
if (attr.name.match(/^data-/)) {
attrs[nameToCamel(attr.name.substr(5))] = attr.value;
}
}
/* TADA */
console.log(attrs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment