javascript - Document.createElement("br") not working with multiple calls to appendChild -
html
var x = document.createelement("p"); var br1 = document.createelement('br'); var br2 = document.createelement('br'); var t5 = document.createtextnode("cse"); var t6 = document.createtextnode("eee"); x.appendchild(t5); x.appendchild(br1); x.appendchild(t6); x.appendchild(br2); document.getelementbyid("new").appendchild(x);
the output should like
cse eee
but output cseeee
the issue here br
element created. unique. @ first when append place in dom, sits in between t5 , t6 element. however, when append br element second time, places in different location in dom , why see result of cseeee followed 1 br element.
you should either omit last one, or clone br element.
var x = document.createelement("p"); var br = document.createelement('br'); var t5=document.createtextnode("cse"); var t6=document.createtextnode("eee"); x.appendchild(t5); x.appendchild(br); x.appendchild(t6); x.appendchild(br.clonenode()); document.getelementbyid("new").appendchild(x);
<div id="new">
Comments
Post a Comment