can't i use for loop with document.getElementById('id').innerHTML | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can't i use for loop with document.getElementById('id').innerHTML

<script> for(var a=5 ; a<55 ; a++){ document.getElementById('text').innerHTML= a } /* result= 54 */ for(var a=5 ; a<55 ; a++){ document.write(a +"<br>") } /* result = 5678910..........54 */ </script> second one result accordingly but first one has 54 as result not a loop. tell me if there is any other conceptual mistake

15th May 2020, 2:16 AM
Divya Mohan
Divya Mohan - avatar
1 Answer
+ 3
Yes, you can manipulate the DOM within a for/ loop. From your example provided the first for/loop replaces the innerHTML with new content for each iteration as opposed to adding to it. If you want to see all 50 numbers you need .innerHTML += a. Please take note of the plus sign in front of the equal sign.
15th May 2020, 2:54 AM
ODLNT
ODLNT - avatar