When updating the content using innerHTML
, it's important to add to the existing content rather than replacing it entirely.
To append new content, use +=
instead of just =
:
function myFunction() {
var bbb = document.getElementById("ilgis").value;
for (i = 0; i <= bbb; i++) {
//document.writeln(aaa);
document.getElementById("demo").innerHTML += Math.ceil((Math.random() * 10) - 1);
}
}
<input type="number" id="ilgis" value="123">
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
EDIT:
Following @Robin Zigmond's suggestion, it is actually more efficient to build a string and then append that string to innerHTML
once after the loop has completed:
function myFunction() {
var bbb = document.getElementById("ilgis").value;
var numbers = "";
for (i = 0; i <= bbb; i++) {
//document.writeln(aaa);
numbers += Math.ceil((Math.random() * 10) - 1);
}
document.getElementById("demo").innerHTML = numbers;
}
<input type="number" id="ilgis" value="123">
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>