WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript HTML DOM Elements (Nodes)


Adding and Removing Nodes (HTML Elements)


Creating New HTML Elements (Nodes)

To add another component to the HTML DOM, you should make the (component hub) first, and afterward annex it to a current component.

 Example

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>

<script>
var para = document.createElement("p");
var hub = document.createTextNode("This is new.");
para.appendChild(node);

var component = document.getElementById("div1");
element.appendChild(para);
</script>
Try it Yourself »

Example Explained 

This code makes another <p> element:

var para = document.createElement("p");

To add content to the <p> component, you should make a content hub first. This code makes a content node:

var hub = document.createTextNode("This is another paragraph.");

Then you should affix the content hub to the <p> element:

para.appendChild(node);

Finally you should affix the new component to a current element.

This code finds a current element:

var component = document.getElementById("div1");

This code affixss the new component to the current element:

element.appendChild(para);

Creating new HTML Elements - insertBefore()

The appendChild() strategy in the past precedent, added the new component as the last offspring of the parent.

If you don't need that you can utilize the insertBefore() technique:

Example

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>

<script>
var para = document.createElement("p");
var hub = document.createTextNode("This is new.");
para.appendChild(node);

var component = document.getElementById("div1");
var tyke = document.getElementById("p1");
element.insertBefore(para,child);
</script>
Try it Yourself »

Removing Existing HTML Elements

To evacuate a HTML component, you should know the parent of the element:

Example

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>

<script>
var parent = document.getElementById("div1");
var youngster = document.getElementById("p1");
parent.removeChild(child);
</script>
Try it Yourself »

Example Explained 

This HTML archive contains a <div> component with two youngster hubs (two <p> elements):

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>

Find the component with id="div1":

var parent = document.getElementById("div1");

Find the <p> component with id="p1":

var youngster = document.getElementById("p1");

Remove the youngster from the parent:

parent.removeChild(child);

Here is a typical workaround: Find the kid you need to evacuate, and utilize its parentNode property to discover the parent:

var kid = document.getElementById("p1");
child.parentNode.removeChild(child);

Replacing HTML Elements 

To supplant a component to the HTML DOM, utilize the replaceChild() method:

Example

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>

<script>
var para = document.createElement("p");
var hub = document.createTextNode("This is new.");
para.appendChild(node);

var parent = document.getElementById("div1");
var tyke = document.getElementById("p1");
parent.replaceChild(para,child);
</script>
Try it Yourself »