showing results for - "add node in javascript"
Axel
11 Oct 2020
1<input type="button" value="Add paragraph node" onclick="add_node()">
2
3    <div id="div12">
4        <p class="bg_c">old paragraph1</p>
5        <p class="bg_c">old paragraph2</p>
6        <p class="bg_c">old paragraph3</p>
7    </div>
8
9
10function add_node() {
11    var parent_div = document.getElementById("div12");
12
13    var nodeToAdd = document.createElement("p");
14    nodeToAdd.setAttribute("class", "bg_c");
15    var newTxt = document.createTextNode("new paragraph1");
16    nodeToAdd.appendChild(newTxt);
17
18    //parent_div.appendChild(nodeToAdd); // is se last position pr ek paragraph ki node ajaegi
19    var target = parent_div.childNodes[3];
20    parent_div.insertBefore(nodeToAdd, target.nextSibling);
21}