-2

Hei guys, i recently started to develop an auto recognizer of all div tags from DOM.

I would like to get their properties, like offsetWidth, offsetLeft, ... as the console return when i try to show the variable.

my code looks like:

var elements = [];

    elements.push(document.querySelectorAll('body > div'));
    console.log(elements);

The console return:

[NodeList[3]]
0: NodeList[3]
0: div
1: div
2: div
length: 3
__proto__: NodeList
length: 1
__proto__: Array[0]

The HTML:

<div style="left:40px;top:10px;position:absolute">
        <img src="assets/images/text1.png" />
    </div>
    <div style="left:200px;top:88px;position:absolute">
        <img src="assets/images/text2.png" />
    </div>
    <div style="left:85px;top:166px;position:absolute">
        <img src="assets/images/text3.png" />
    </div>

My question is:

How i can access the array with my divs so i can get its length, its whole properties (like offsetLeft) if the expanded div in console shows properties like x,y,naturalHeight and many others...

EDIT

Sorry, i forgot to mention, the code should be PURE JS! Sorry for the wrong jQuery tag added...

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
OzZie
  • 523
  • 9
  • 21
  • I think this question addresses your question http://stackoverflow.com/questions/14645806/get-all-attributes-of-an-element-using-jquery – sbonkosky Jul 24 '15 at 19:25
  • 2
    With pure javascript I think this question answers it http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery – sbonkosky Jul 24 '15 at 19:35
  • @sbonkosky yeap! this one does the trick! Thanks – OzZie Jul 24 '15 at 19:37

2 Answers2

0

You are actually initiating an array and pushing the elements from the querySelector to it. Instead you can directly assign the result of querySelector

var elements = document.querySelectorAll('body > div');
console.log(elements);
Ammadu
  • 1,675
  • 15
  • 17
0

First of all, why would you use .push() with the array if you can directly declare it and initialize it with the elements:

 var elements = document.querySelectorAll('body > div');

Then for each element you will need to use .attributes to get all the attributes:

 for(elem in elements) {
     console.dit(elem.attributes);
 };
cнŝdk
  • 31,391
  • 7
  • 56
  • 78