0
// Find all posts
var posts = node.getElementsByClassName("userContentWrapper");
var post, text;
console.log(posts);
console.log(typeof(posts));
console.log(Object.keys(posts));
console.log(posts.length);

The above outputs: enter image description here

:( I don't understand. Why is length 0? It works in a fiddle: http://jsfiddle.net/p7yfv37s/

I also printed the object again after the length, and it still shows length: 4. enter image description here

sihrc
  • 2,728
  • 2
  • 22
  • 43
  • Can you reproduce this in a fiddle ? – Denys Séguret Jun 04 '15 at 15:54
  • @DenysSéguret I'll give it a shot. Give me a few minutes. It seems to work properly in a jsFiddle. :( http://jsfiddle.net/p7yfv37s/ – sihrc Jun 04 '15 at 15:55
  • @Donal I'm not sure what you mean. How does that affect what .length logs compared to what the object logs. – sihrc Jun 04 '15 at 16:00
  • The DOM can't be magically updated between two lines of JS code. That's why I asked for a fiddle. – Denys Séguret Jun 04 '15 at 16:01
  • I voted to close for "no reproduction". @sihrc I suggest your delete your question and make a new one if you find the conditions of reproduction (and don't find the explanation on your own). – Denys Séguret Jun 04 '15 at 16:06

1 Answers1

1

If you put your cursor over the i icon you will see that HtmlCollection state is captured only upon first expansion. It does not show its state at the moment of console.log invocation.

enter image description here

Most likely logging is performed before DOM is loaded or node is dynamically populated by javascript. HTMLCollection is kind of live collection which track all DOM changes (more info about difference between HTMLCollection and NodeList)

See simple example: http://jsfiddle.net/p7yfv37s/1/

Community
  • 1
  • 1
Eugene
  • 722
  • 6
  • 19
  • Is the way around this then to use some sort of callback? – sihrc Jun 04 '15 at 20:26
  • Yes, but I can't give more info since your question does not provide details about what is causing DOM changes. – Eugene Jun 04 '15 at 22:11
  • It's the Facebook feed. I have a mutation observer on the news feed and I want to capture posts as they populate to do some analysis. – sihrc Jun 05 '15 at 04:34