0

I am little confused. When i try to get elements like document.getElementsByClassName('html5gallery-tn-image-0') what i get is that: enter image description here

These are the elements i am searching for but instead the array says that it is empty. Can you explain me please why is that way and can i reach the elements in this array? Thank you in advance!

5
  • 5
    It is not an array, but a node list (array-like). That looks different in dev tools. It is not empty. Commented Jul 29, 2017 at 9:23
  • Logical! Thank you very much! Commented Jul 29, 2017 at 9:25
  • Try this: document.getElementsByClassName('html5gallery-tn-image-0')[0] and let me know what you get. Commented Jul 29, 2017 at 9:30
  • I've already answered to a similar question stackoverflow.com/questions/45077222/… Commented Jul 29, 2017 at 9:30
  • Nothing again :/ Commented Jul 29, 2017 at 9:35

3 Answers 3

2

You have several things going on that explains this behaviour:

  1. You are performing the console.log at a moment that there are no such elements yet in the document. Probably the JavaScript executes before the document is ready. This explains why temp[0] is undefined.

  2. The HTMLCollection returned by getElementsByClassName is not an array and has some magical behaviour: it is a live collection. So if the document gets an additional element of that class, it will magically appear in that temp collection without you touching it!

  3. When you log an object to console, the Chrome dev tools will not collect all the object properties at that moment, but asynchronously. This means that although at the time of logging the collection was empty, it no longer is when you click on the dev tools to see what is in the collection.

See the first two points illustrated in this script:

var temp = document.getElementsByClassName('html5gallery-tn-image-0');
console.log(temp.length); // 0

// add the class to the element
mydiv.className = "html5gallery-tn-image-0";
console.log(temp.length); // 1
<div id="mydiv"></div>

Solution

Move your JavaScript code so that it only executes when the document is completely loaded. Either:

  • Move your Javascript to the end of the body tag, or
  • wrap the code in a document.addEventListener('DOMContentLoaded', function () { ... }); callback.
Sign up to request clarification or add additional context in comments.

Comments

0

In your console.log it returns with two image elements on it, so it not empty.

1 Comment

[] is an array, {} would be an object.
0

getElementsByClassName returns a HTMLCollection, which has a few special properties. One of them is, that the content of that array-like thing gets updated as soon as the DOM gets updated. Additionally in Chrome the part which shows "[]" got calculated as soon as you pressed enter, but the content (those two elements) get evaluated only when you expand the output.

Comments

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.