<body>
<div id = "app">TODO... This is an HTML5 Template. Put your own content here.</div>
<script>
var Para = prompt("How many paragraph elements you want?");
var element;
for(i = 0; i < Para; i++){
element = prompt("Provide the element you want to fill up the Paragraph above");
}
document.getElementById("app").innerHTML = element;
</script>
</body>
Asked
Active
Viewed 102 times
-2

j08691
- 204,283
- 31
- 260
- 272

Oscar Agbasi
- 1
- 1
1 Answers
0
You should be doing something like this:
document.getElementById("app").insertAdjacentHTML('beforeend', element);
As stated by @Makyen, you shouldn't be using innerHTML +=
as I stated in my first answer. That would probably create issues in your application, please follow the link in the comments to get more information.
And probably, the line of code I wrote should be inside the for
loop.
for(i = 0; i < Para; i++){
element = prompt("Provide the element you want to fill up the Paragraph above");
document.getElementById("app").insertAdjacentHTML('beforeend', element);
}

f-CJ
- 4,235
- 2
- 30
- 28
-
@OscarAgbasi (&f-CJ) Using `.innerHTML +=` is *almost always* **bad**. Don't do it. Don't be in the habit of doing it. Here is some explanation: [In-page JavaScript stops when I use document.body.innerHTML += newHtmlText](https://stackoverflow.com/q/45215006) If you are going to do something like this then you should use [`.insertAdjacentHTML()`](//developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML). In this instance it would be `appElement.insertAdjacentHTML('beforeend', element)`. Note: you should do `document.getElementById("app")` only once and store it into a variable. – Makyen Aug 09 '18 at 01:22
-
'+prompt("Provide the element you want to fill up the Paragraph above")+'
'` – Sphinx Aug 08 '18 at 22:06