javascript - How to add Elements to DOM -
i need function creates element , adds text element , adds new element location in dom. noob this. find function don't know how automaticaly specify location can call function , example specify third argument element want append new element.
function appendelement (elemnode,textnode) { var element = document.createelement(elemnode); var text = document.createtextnode(textnode); element.appendchild(text); document.body.appendchild(element); } appendelement("b","lorem");
here's way in 1 line:
function appendelement (elemnode, textcontent, parent) { parent.appendchild(document.createelement(elemnode)).textcontent = textcontent; } appendelement("b","lorem", document.getelementbyid("container"));
div { background-color: aqua }
<div id="container"></div>
Comments
Post a Comment