doesn't work the "for" loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

doesn't work the "for" loop

Help me plz! Why doesn't the "for" loop work? I press the button, shows the first "div", shows the length of the array, equal to two. and does not go further <!doctype html> <html> <head> </head> <body> <div class="first"> <p>div1p1</p> <p>div1p2</p> <p>div1p3</p> <button onclick="inner()">click</button> </div> <div class="first"> <p>div2p1</p> <p>div2p2</p> <p>div2p3</p> <p>div2p4</p> </div> <div class="third"> <p>div3p1</p> <p>div3p2</p> </div> <script> function inner(){ var fir=document.getElementsByClassName("first"); for(var i=0;i<fir.length;i++){ alert(fir[i].innerHTML); document.write(fir.length); } } </script> </body> </html> https://code.sololearn.com/WTlDA3Df0GtZ/?ref=app

9th Jun 2019, 11:07 AM
Pavel Makhov
Pavel Makhov - avatar
2 Answers
0
Try the same without document.write and it works... Guessing that document.write write over your script so no for loop anymore xD
9th Jun 2019, 2:13 PM
Dyf Dyf
Dyf Dyf - avatar
0
Thank you! why is this kind of code not working? document.write instead of alert https://code.sololearn.com/WCGccFLi86ay/?ref=app
10th Jun 2019, 7:56 AM
Pavel Makhov
Pavel Makhov - avatar