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"
8
  • 1
    If you think that's too hard, maybe you should seriously reconsider your employment. Commented Oct 25, 2010 at 3:16
  • I'm inclined to agree with @Tate. Commented Oct 25, 2010 at 3:17
  • Are all you attributes of the form attr<number>? Of you wrote that just as an example? Commented Oct 25, 2010 at 3:20
  • oh, sorry for my Question.. everyone's begin with zero ^^. i'm learning and i was stacked.. Commented Oct 25, 2010 at 3:20
  • So you just want to get attr0, attr1, attr2 ... ? Commented Oct 25, 2010 at 3:22

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Why do you assume they have that format? And even so they're 1-indexed, not 0-index as you implemented.
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.
yea.. I forgot somethink.. if in EX: instead 'att1' = 'a' ; 'att2' = e; ' att3 ' = c. How do you resolve this ? TO @Alin : you're right!. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.