Skip to content

Instantly share code, notes, and snippets.

@duzun
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duzun/b49fb4ece4aeda44b4d5 to your computer and use it in GitHub Desktop.
Save duzun/b49fb4ece4aeda44b4d5 to your computer and use it in GitHub Desktop.
jQuery.fn.getAttributes
/*
source: http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery/16935800#16935800
*/
(function($) {
$.fn.getAttributes = function () {
var elem = this,
attr = {};
if(elem && elem.length) $.each(elem.get(0).attributes, function(v,n) {
n = n.nodeName||n.name;
v = elem.attr(n); // relay on $.fn.attr, it makes some filtering and checks
// could use elem.prop(n) here
if(v != undefined && v !== false) attr[n] = v;
});
return attr;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment