+ 2

For loop

Are document.getElementById are alway written outside of the executed loop due to the quotation (" ") in the variables text? I thought they were usually written inside the executed loop for example: <p id="demo"><p> <script> var text = " "; (<--- talking about this) var food = ["chicken", "salad", "crouton"]; var a; for (a = 0; a < food.length; a++) { text += "I love" + food[a] + "<br>"; } document.getElementById("demo").innerHTML = text; </script>

27th Dec 2016, 7:59 AM
totoro
totoro - avatar
1 Odpowiedź
+ 4
Both is possible, you could also do : var food = ["chicken", "salad", "crouton"]; for (var a=0; a<food.length; a++) { document.getElementById("demo").innerHTML += "I love " + food[a] + "<br>"; } Gives you the exact same result.
27th Dec 2016, 9:10 AM
Alireza M
Alireza M - avatar