Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript HTML DOM Elements (Nodes)

JavaScript HTML DOM Elements (Nodes)

This chapter introduces how to add and remove elements (nodes) in a document.


Create a New HTML Element (Node) - appendChild()

To create a new HTML element (node), first create an element, then add 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

The following code is used to create a

element:

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

is

element to create a new text node:

var node = document.createTextNode("这是一个新的段落。");

Add the text node to the

element:

para.appendChild(node);

Finally, add the p element to an existing element.

Find the existing element:

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

Add it to the existing element:

element.appendChild(para);


Create a New HTML Element (Node) - insertBefore()

In the above example we used the appendChild() method, which is used to add a new element to the end.

If we need to add a new element to the beginning, we 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 »


Removing an Existing Element

To remove an element, you need to know its parent 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 »

**Note:** Early Internet Explorer browsers do not support the node.remove() method.

Example Parsing

In an HTML document, the

element contains two child nodes (two

element):

这是一个段落。

这是另外一个段落。

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 node from the parent element:

parent.removeChild(child);

| lamp | It would be nice to be able to delete an element without referencing its parent element.
Unfortunately, that is not possible. The DOM needs to know the element you want to delete, as well as its parent element. | | --- | --- |

The following code first finds the child element to be deleted, then finds its parent element, and then deletes the child element (deleting a node requires knowing the parent node):

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

child.parentNode.removeChild(child);


Replacing HTML Elements - replaceChild()

We can use the replaceChild() method to replace an element in the HTML DOM.

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 parent = document.getElementById("div1"); var child = document.getElementById("p1"); parent.replaceChild(para, child);

script

Try it »


HTML DOM Tutorial

In the HTML DOM section of our JavaScript tutorial, you have learned:

  • How to change the content of an HTML element (innerHTML)
  • How to change the style of an HTML element (CSS)
  • How to react to HTML DOM events
  • How to add or remove HTML elements

If you want to learn more about accessing the HTML DOM with JavaScript, please visit our complete HTML DOM Tutorial。