3

Possible Duplicate:
Get all Attributes from a HTML element with Javascript/jQuery

I need to retrieve all attributes of a DOM element. I've seen the getAttribute() method, but I don't know the names of the attributes in advance. If I use getElementById() to retrieve an element, how do I then access all attributes of that element and their values?

Community
  • 1
  • 1
Lee Grey
  • 303
  • 1
  • 5
  • 16
  • Yeah, I found what I needed at [Get all Attributes from a HTML element with Javascript/jQuery](http://stackoverflow.com/questions/828311/how-to-iterate-through-all-attributes-in-an-html-element?rq=1). Sorry. – Lee Grey Aug 17 '12 at 23:51

1 Answers1

10

Each DOM node has an attributes property, which is a NamedNodeMap (essentially an array with a few extra features). In particular this means you can get elem.attributes.length and loop through them.

Each individual attribute is an Attr object, which has (among other things) name and value properties.

Note that IE7 and below have a list of all attributes that could possibly be defined (84 in all) whether or not they are actually on the element. You may want to run a quick check for falsy values before actually including an attribute value.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592