2

following is my HTML

HTML :

<div Attr1 = "val1" Attr2 = "val2" Attr3 = "val3"  >
       Hello !!
</div>

I want to extract all the attributes (& its corresponding values) of an element. How can I do this using jQuery?

I tried something like below,

$(function() {
   for(var i in $('div')[0].attributes) {
     console.log($('div')[0].attributes)[i] , $('div')[0].attributes)[i].value);
   }
});

Are there any more possible ways for doing this ?

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
  • 1
    Duplicate: I think you're looking for Iterating over element attributes with jQuery - http://stackoverflow.com/questions/2224933/iterating-over-element-attributes-with-jquery – Richard JP Le Guen Aug 06 '10 at 19:17
  • Also, assuming they're not just examples, your HTML won't validate as these are not valid attributes, which is inadvisable. If you really need these attributes `Attr1`, `Attr2`, `Attr3` look into the HTML5 `data-` attributes: http://html5doctor.com/html5-custom-data-attributes/ – Richard JP Le Guen Aug 06 '10 at 19:19
  • @Richard Those Attributes aren't the real attribute, I do Agree with you those aren't valid. Here I want to express my Question so I used some dummy `attr's`. sound silly?? –  Aug 06 '10 at 19:26
  • no, it makes perfect sense; I was just being thorough. – Richard JP Le Guen Aug 06 '10 at 19:48

1 Answers1

0

You are looking for getAttributes jQuery Plugin.

A simple and small (490 bytes minified) plugin to retrieve all attributes from an element (useful if you have to work with markup that you don't have control over). It returns an object that can be used as the argument to .attr()

Example:

console.log($.getAttributes('div'), true);

Update:

From jQuery Docs:

Whenever you use jQuery's each-method, the context of your callback is set to a DOM element. That is also the case for event handlers.

Meaning you can do:

var attrs = $("div")[0].attributes;

for(var i = 0; i < attrs.length; i++) {
  alert(attrs[i].nodeName + " => " + attrs[i].nodeValue);
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Thanks for the link, But I'm not willing to do this using a plugin. Are there anymore ways ? –  Aug 06 '10 at 19:19
  • 1
    Why wouldn't you want to use a plugin? At the very least, open the plugin and see how they did it. – Soviut Aug 06 '10 at 19:25
  • @Avinash: I would still suggest you to use the plugin, note also that it is only 490 bytes. – Sarfraz Aug 06 '10 at 19:27
  • 1
    It seems odd that you don't want to use a plugin. Plugins are just jquery code. You could just look at the plugin source and see how they do it. – Gregg Aug 06 '10 at 21:41
  • @Soviut , @Gregg Did u guys had seen this particular plugin functionality, it generates only the attributes & values which are `events`. Is there anything wrong in learning a(**new**) thing (though the code is already available) ?? Don't know why your comments are getting upvoted. seems like ppl around here are getting well used to plugin for every thing rather than implementing their own thought. I'm sorry if this hurts :( –  Aug 07 '10 at 09:48
  • The getAttributes plugin mentioned only looks for a set of predefined attributes. This is not useful for other attributes such as HTML5's "data-" (data dash) attributes which are customizable. Also, $("div")[0].attributes has issues in IE 8 and probably older IE versions. IE 9 handles it properly. – Silkster Aug 30 '11 at 18:32