Skip to content

Instantly share code, notes, and snippets.

@jmmcduffie
Last active August 29, 2015 14:18
Show Gist options
  • Save jmmcduffie/26577fc8b5d842888e88 to your computer and use it in GitHub Desktop.
Save jmmcduffie/26577fc8b5d842888e88 to your computer and use it in GitHub Desktop.
Data Attribute Options
// Usage:
//>: extractDataOptions( $("<div data-foo='opt1:bar opt2:baz'/>").data("foo") )
//<: { opt1: "bar", opt2: "baz" }
function extractDataOptions(attr) {
var options = {};
if (!!attr) {
attr.split(" ").forEach(function(option) {
var pieces = option.split(/:(.+)?/),
key = pieces[0],
value = pieces[1];
options[key] = value;
});
}
return options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment