Ahmed Abbas Ahmed Abbas - 4 months ago 9
Javascript Question

Problems with for loop not printing with innerHTML

Whenever I try to print any statement using a for loop, it does not print using

document.getElementById("").innerHTML=;
- instead it prints using
document.writeln();


Why?



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>For Loop</title>
</head>

<body>
<p id="demo"></p>

</body>
</html>

<script>
var i;

for(i=0;i<10;i++){
document.getElementById("demo").innerHTML = "This is Insane";
}

</script>




Answer

You should use document.getElementById("").innerHTML+=""; now whenever loop will run,data will be incremented.

Comments