add node in javascript code example

Example: 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);
}