Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLCollection.item() function returns null

In an angular app I am using plain JS to get the elements by class name Code:

var testElements = document.getElementsByClassName("project-link");
console.log(testElements);

this prints as such:

[item: function, namedItem: function]
0: a.project-link.ng-binding
1: a.project-link.ng-binding
...
30: a.project-link.ng-binding
length: 32
__proto__: HTMLCollection

However. if I try printing an item alone, this returns null:

console.log(testElements.item(4));
null

I don't understand what is wrong with this code, I have also tried the Array.prototype.filter.call function and it doesn't work either. Any ideas?

like image 383
Ioana Grozav Avatar asked Jun 21 '26 03:06

Ioana Grozav


1 Answers

I fixed this bug by adding my code inside the window load function:

window.addEventListener("load", function(event) {
   // your code ....
 });
like image 92
Edhar Dowbak Avatar answered Jun 22 '26 18:06

Edhar Dowbak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!