Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created September 29, 2011 11:36
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 barneycarroll/1250575 to your computer and use it in GitHub Desktop.
Save barneycarroll/1250575 to your computer and use it in GitHub Desktop.
Get all an element's attributes & values as a matrix
// Return all attributes, optionally filtered by RegExp
$.fn.attrs = function(expr){
var
el = this[0],
$el = this,
attrArray = [];
for(var i=0, attrs = el.attributes; i<attrs.length; ++i){
var attrKey = [
attrs.item(i).nodeName,
attrs.item(i).nodeValue
];
if(!expr || expr.test(attrKey[0]))
attrArray.push(attrKey);
};
return attrArray;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment