Last active
August 29, 2015 14:18
-
-
Save jmmcduffie/26577fc8b5d842888e88 to your computer and use it in GitHub Desktop.
Data Attribute Options
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
// 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