Answers for "javascript add node to dom"

11

how to add elements in javascript html

//adding 'p' tag to body

  var tag = document.createElement("p"); // <p></p>
  var text = document.createTextNode("TEST TEXT"); 
  tag.appendChild(text); // <p>TEST TEXT</p>
  var element = document.getElementsByTagName("body")[0];
  element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>
Posted by: Guest on May-31-2020
0

add node in javascript

<input type="button" value="Add paragraph node" onclick="add_node()">

    <div id="div12">
        <p class="bg_c">old paragraph1</p>
        <p class="bg_c">old paragraph2</p>
        <p class="bg_c">old paragraph3</p>
    </div>


function add_node() {
    var parent_div = document.getElementById("div12");

    var nodeToAdd = document.createElement("p");
    nodeToAdd.setAttribute("class", "bg_c");
    var newTxt = document.createTextNode("new paragraph1");
    nodeToAdd.appendChild(newTxt);

    //parent_div.appendChild(nodeToAdd); // is se last position pr ek paragraph ki node ajaegi
    var target = parent_div.childNodes[3];
    parent_div.insertBefore(nodeToAdd, target.nextSibling);
}
Posted by: Guest on June-04-2021

Code answers related to "javascript add node to dom"

Code answers related to "Javascript"

Browse Popular Code Answers by Language