jQuery Output All Attributes of Element

By Sam Deering

jQuery code snippet which outputs all attributes of element using the default .attr() function on any dom element(s).

//extending the attr function to return all attrs
  (function($) {
          // duck-punching to make attr() return a map
          var _old = $.fn.attr;
          $.fn.attr = function() {
            var a, aLength, attributes, map;
            if (this[0] && arguments.length === 0) {
                    map = {};
                    attributes = this[0].attributes;
                    aLength = attributes.length;
                    for (a = 0; a < aLength; a++) {
                            map[attributes[a].name.toLowerCase()] = attributes[a].value;
                    return map;
            } else {
                    return _old.apply(this, arguments);





Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

  • sash

    how do i use this snippet?

  • Zlosny

    thanks! this helped me a lot.

  • avi

    && = &&

    • avi

      meant that amp; amp; is &&

Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.