Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't access HTMLCollection

Tags:

javascript

I can't access HTMLCollection Type When I use getElementsByClassName.

I wanna get the length but I can't get that

var documentHeader = parent.document.all['header'];
var motionClass = documentHeader.getElementsByClassName('motion');

this is a result of motionClass

HTMLCollection []
 0: div.motion
 length: 1
 __proto__ : HTMLColletion

If I access length result is 0

How can I result This Issue?

like image 954
EddyKim Avatar asked Nov 04 '25 17:11

EddyKim


1 Answers

NOTE: Here we execute the code safely when the DOM is ready. Ensure you are doing that.

document.addEventListener('DOMContentLoaded', function() {
  let motionArray = document.getElementsByClassName('motion');
  console.log(motionArray.length);
});
<div class='motion'>test</div>
<div class='motion'>test</div>
<div class='motion'>test</div>
<div class='motion'>test</div>
<div class='motion'>test</div>
<div class='motion'>test</div>
<div class='motion'>test</div>
like image 112
Bibberty Avatar answered Nov 06 '25 11:11

Bibberty



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!