Skip to main content
2 of 5
added 331 characters in body
vivek
  • 298
  • 6
  • 16

Get all the attributes of a DOM Node

I needed this small function for one of my project.

Fetch Attributes

function fetchAttrs(node) {
  
  if (!node || !node.hasAttributes()) return {};
  var attrs = node.attributes;
  var res = {};
  var length = attrs.length;
  for (i = 0; i < length; i++) {
    var attr = attrs[i];
    res[attr.name] = attr.value;
  }
  return res;
}

Its working fine as expected but I wanted to know if I am missing something from performance point of view or good coding practices. Just need a second opinion :)

vivek
  • 298
  • 6
  • 16