Skip to content

Instantly share code, notes, and snippets.

@happyboredom
Created January 4, 2012 19:21
Show Gist options
  • Save happyboredom/1561564 to your computer and use it in GitHub Desktop.
Save happyboredom/1561564 to your computer and use it in GitHub Desktop.
bonzo - convert .data() from a DOM element into URL encoded string (as with ".param()" in jQuery)
/*
Takes data elements from an HTML tag and converts to a URL encoded string.
*/
bonzo.aug({
param: function () {
var str = [];
obj = this.data();
for(var p in obj)
str.push(p + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
})
/*
example:
<a href="#" data-color="red" data-size="small">Car</a>
bonzo(qwery('a')).param()
Note: I am used qwery for DOM selectors.
returns:
"color=red&size=small"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment