javascript - Appending new paragraphs -
been stuck on while now, trying add multiple new p elements div adding content first 1 instead of creating new one.
var p = document.createelement("p"); var output = document.getelementbyid('output'); function on button press
p.appendchild(document.createtextnode("hello"+"\n")); output.appendchild(p); thanks in advance, need solution i'm allowed infinite amount of new paragraphs until condition met.
the problem create 1 paragraph append multiple text nodes in it. despite on how looks, output.appendchild(p) doesn't append initial p more once. in fact, if element in dom (like in case afther first click), appendchild moves element new location. in case new location same original. result, create new text node every click.
you need create new htmlparagraphelement on every click:
document.queryselector('button').onclick = function() { var p = document.createelement("p"); var output = document.getelementbyid('output'); p.appendchild(document.createtextnode("hello"+"\n")); output.appendchild(p); }; <button>click</button> <div id="output"></div>
Comments
Post a Comment