<h3>This is a practice set for manipulating nodes.</h3>
<p>This is a simple exercise to create a new element,a text node,appending a node and inserting a new node.</p>
<p>When 'Change Text' Button is Clicked the Ouput of this practice set will be Hello World.</p>
<p id="domtree">Hi There</p>
<div id="Change Text" class="button">Change Text</div>
//Here we are creating a new element "p"
var button = document.getElementById("Change Text");
button.onclick = function () {
var newEl = document.createElement("p");
//use document.createTextNode(text) to create a new text node and also an ElementNode.appendChild(elementToAppend) to append the created element into an existing element.
newEl.appendChild(document.createTextNode("Hello, World"));
document.body.appendChild(newEl);
//Inserting a new node using insertBefore() method which inserts a node as a child, right before an existing child
var domtreeEl = document.getElementById("domtree");
document.body.insertBefore(newEl, domtreeEl);
// we are removing a child node from a parent node using removeChild()
/* var el = document.getElementById("domtree");
console.log(typeof el);
document.body.removeChild(el);*/
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.