0

When outputting this code in the console;

var getmythingselems = document.getElementsByClassName("getmythings");
console.log(getmythingselems );

This is returned;

[item: function, namedItem: function]
0: div.gb_ja
length: 1
__proto__: HTMLCollection

But when I try to access the length of the object like below;

console.log(getmythingselems.length)

It returns 0, am I accessing this the correct way?

Note: If I put Settimeout on this for a good few seconds it'll work fine but I'm just confused as to why it would show me some elements initially and then doesn't let me access it.

Edit: Heres the entire code;

var getmythingselems = document.getElementsByClassName("getmythings");
console.log(getmythingselems);

var thethingiwant = "";

if (getmythingselems != null && getmythingselems.length > 0) {
    thethingiwant = getmythingselems[0].textContent;
}
console.log(thethingiwant);

return thethingiwant ;
r0bb077
  • 686
  • 2
  • 11
  • 30
  • Please post a complete code example. – j08691 Oct 14 '14 at 16:56
  • Sounds like you are trying to access the data before it is rendered on the page. Use document ready or window onload or place the code after the element or after you dynamically load it. – epascarello Oct 14 '14 at 17:06
  • `getElementsByClassName` returns a **live** collection. It might be filled only later. You seem to have a mix of [this problem](http://stackoverflow.com/q/14028959/1048572) with [this oddity](http://stackoverflow.com/q/4057440/1048572) – Bergi Oct 14 '14 at 17:07

0 Answers0