DOM element



Create a new HTML element


Create a new HTML element

To add a new element to the HTML DOM, you must first create the element (element node) , and then appends the element to an existing element.


Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<div id="div1">
<p id="p1">这是一个段落。</p>
<p id="p2">这是另一个段落。</p>
</div>
<script>
var para=document.createElement("p");
var node=document.createTextNode("这是一个新段落。");
para.appendChild(node);
var element=document.getElementById("div1");
element.appendChild(para);
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance


Example analysis:

This code creates a new <p> element:

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

To add text to the <p> element, 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 add < The p> element appends this text node:

para.appendChild(node);

Finally you must append the new element to an existing element.

This code finds an existing element:

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

The following code adds a new element after the existing element:

element.appendChild(para);


Delete existing HTML elements

The following code demonstrates how to delete elements:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<div id="div1">
	<p id="p1">这是一个段落。</p>
	<p id="p2">这是另一个段落。</p>
</div>
<script>
var parent=document.getElementById("div1");
var child=document.getElementById("p1");
parent.removeChild(child);
</script>

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance


Instance analysis

This HTML document contains a <div> element with two child nodes (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 element with id="div1":

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

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

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

Remove child elements from parent elements:

parent.removeChild(child);

lampIt would be nice if I could delete an element without referencing the parent element.
But it’s a pity. The DOM needs to know the element you need to delete, and its parent element.

This is a common solution: find the child element you wish to remove, then use its parentNode property to find the parent element:

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


HTML DOM Tutorial

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

  • How to change the content of an HTML element (innerHTML)

  • How to change an HTML element Styles (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 using JavaScript, visit our complete HTML DOM tutorial.