John Mann
John Mann

Reputation: 83

How to display number of images user requests? Javascript

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

Answers (1)

Sorikairo
Sorikairo

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

Related Questions