HTML DOM - Elements
Add, delete, and replace HTML elements.
Create a New HTML Element - createElement()
To add a new element to the HTML DOM, you must first create the element and then append it to an existing element.
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 node=document.createTextNode("This is a new paragraph."); para.appendChild(node); var element=document.getElementById("div1"); element.appendChild(para);
script
Try it »
Example Parsing
This code creates a new
element:
var para=document.createElement("p");
To add to the
element's text, you must first create a text node. This code creates a text node:
var node=document.createTextNode("This is a new paragraph.");
Then you must append to the
element the text node:
para.appendChild(node);
Finally, you must append this new element to an existing element.
This code finds an existing element:
var element=document.getElementById("div1");
This code appends the new element to this existing element:
element.appendChild(para);
Create a New HTML Element - insertBefore()
The appendChild() method in the previous example adds the new element as the last child of the parent element.
If you do not want this, you can use the insertBefore() 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 node=document.createTextNode("This is a new paragraph."); para.appendChild(node); var element=document.getElementById("div1"); var child=document.getElementById("p1"); element.insertBefore(para,child);
script
Try it »
Remove an Existing HTML Element
To remove an HTML element, you must know the parent element of that 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 child=document.getElementById("p1"); parent.removeChild(child);
script
Try it »
Example Parsing
This HTML document contains an element with two child nodes (two
elements):
This is a paragraph.
This is another paragraph.
Find the element with id="div1":
var parent=document.getElementById("div1");
Find the element with id="p1"
element:
var child=document.getElementById("p1");
Remove the child element from the parent element:
parent.removeChild(child);
|
| Can you remove an element without referencing its parent element?
Sorry. The DOM needs to know the element you want to remove, as well as its parent element. | | --- | --- |
Here is a common workaround: find the child element you want to remove, then use the parentNode property to find its parent element:
var child=document.getElementById("p1");
child.parentNode.removeChild(child);
Replace an HTML Element
To replace an element in the HTML DOM, use 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 parent=document.getElementById("div1"); var child=document.getElementById("p1"); var para=document.createElement("p"); var node=document.createTextNode("This is a new paragraph."); para.appendChild(node); parent.replaceChild(para,child);
script
Try it »