The task has the form:

Create a page with a button; when you click on a button, a paragraph with arbitrary text should be created on the page. After creating 10 paragraphs, all of them should be deleted.

Do I have a hitch like after creating 10 paragraphs delete them ?!

function createNewPar() { var list = document.getElementById("test"); var item = document.createElement("p"); item.innerHTML = "NEW ITEM"; list.appendChild(item); } 
 <div id="test"></div> <input type="button" value="Add Par" onclick="createNewPar()" /> 

  • jQuery is on the site? Or some other JS framework? Or are you trying to implement it using standard javascript tools? - cyadvert
  • What have you tried to do to solve the problem? What exactly did not work out? The solution of learning tasks for students is not very welcome on this site. - Dmitriy Simushev
  • on pure js. I don’t know how to erase 10 paragraphs at once - user191427
  • unless document.getElementById ("test"). innerHTML = "" does not give a ride? - cache
  • I tried, but it didn’t work out that way ... - user191427

1 answer 1

There are several ways to remove child nodes. For example, you can manually bypass all child nodes and delete them:

 var container = document.getElementById('container'); while (container.firstChild) { container.removeChild(container.firstChild); } 

I will not give you a detailed solution to your educational problem, since you must solve it yourself. That's what she is learning .

  • thanks for the help! - user191427
  • @user191427, if I answered your question correctly, mark the answer as "correct." - Dmitriy Simushev