Reputation: 83
I am trying to take a number from a user and display the corresponding number of images. I have no idea why my code is broken. I cannot find the answer anywhere. Please help.
<!DOCTYPE hmtl>
<html>
<head>
<script>
function howMany(){
var numBurgers = parseInt(prompt('How many burgers do you want?', "3"));
var x = "";
for(var count = 0; count < numBurgers; count++;){
x += "<img src=\"burger.jpg\" />";
}
document.getElementById('burgerImages').innerHTML = x;
}
</script>
</head>
<body onload="howMany();">
<div id="burgerImages"></div>
</body>
Upvotes: 0
Views: 17
Reputation: 798
You are doing
document.getElementById('burgerImages').innerHTML = x;
Which overwrite the content of burgerImages with x.
You should use the appendChild method as following :
document.getElementById('burgerImages').appendChild(x).
Upvotes: 1