1

I have a HTML DIV element:

<div class="obj" height="this is attr 1" rel="this is att2" width="this is att3"></div>

I've a new Variable: attArray:

var attArray = new Array();

I want to get step by step each att in div.obj into attArray. How do I do it?

attArray[0] = "this is attr1"
attArray[1] = "this is attr2"
attArray[2] = "this is attr3"
Jacob Relkin
  • 156,685
  • 31
  • 339
  • 316
Rueta
  • 3,057
  • 2
  • 22
  • 20

2 Answers2

4

Each element already has an attributes-collection, you can access it like an array.

Dr.Molle
  • 115,009
  • 15
  • 189
  • 197
2

Simple:

$('.obj').each(function() {
   var attArray = [];
   for(var k = 0; k < this.attributes.length; k++) {
       var attr = this.attributes[k];
       if(attr.name != 'class')
          attArray.push(attr.value);
   }
   //do something with attArray here...
});

Working example

Jacob Relkin
  • 156,685
  • 31
  • 339
  • 316
  • Why do you assume they have that format? And even so they're 1-indexed, not 0-index as you implemented. – Alin Purcaru Oct 25 '10 at 03:19
  • I see now, but I still think (or like to think) he wants any element attribute (class, id, title, etc.). Otherwise it's plain boring. – Alin Purcaru Oct 25 '10 at 03:21
  • yea.. I forgot somethink.. if in EX: instead 'att1' = 'a' ; 'att2' = e; ' att3 ' = c. How do you resolve this ? TO @Alin : you're right!. :) – Rueta Oct 25 '10 at 03:22