while-loop problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

while-loop problem

Hey Guys, I have a problem with the following code: var x = document.getElementById('Auswahl').value; var activeElements = document.querySelectorAll('#class1').length; while(activeElements < x){ document.getElementsByClassName("wrapper")[0].appendChild(cln); activeElements = document.querySelectorAll('#class1').length; if(activeElements == x){ break; } } In this loop my input is 3. The loop should run until i have 3 Elements named #class1. But the loop never stops. It works with the input 2 but not with 3 or 4. Can anybody help me out?

13th Jul 2020, 11:48 AM
Julian Dörling
Julian Dörling - avatar
4 Answers
0
@Rithea Sreng after some debbuging i found out that this line "document.getElementsByClassName("wrapper")[0].appendChild(cln);" is only executet 1 time, After that this line does not work anymore. at the beginning i have tried it with activeElements++ but it didn't work.
13th Jul 2020, 12:27 PM
Julian Dörling
Julian Dörling - avatar
0
NVM I found the problem
13th Jul 2020, 12:30 PM
Julian Dörling
Julian Dörling - avatar
0
How did you solve it?
13th Jul 2020, 1:08 PM
HonFu
HonFu - avatar
0
document.getElementsByClassName("wrapper")[0].appendChild(cln); This line is like Strg+V but only works one time. So i put two code lines (which are not included in the code above) into the while loop and then i could copy it infinite times.
13th Jul 2020, 1:15 PM
Julian Dörling
Julian Dörling - avatar