ขั้นตอนเพิ่มอีเลเมนต์ใหม่: 1) สร้างอีเลเมนต์ด้วย document.createElement() 2) สร้างข้อความด้วย document.createTextNode() 3) ผูกข้อความเข้ากับอีเลเมนต์ด้วย appendChild() 4) ผูกอีเลเมนต์เข้ากับ parent ด้วย appendChild()
ตัวอย่าง
<html> <body>
<div id="new">
<p id="p1">Tutorix</p>
<p id="p2">Tutorialspoint</p>
</div>
<script>
var tag = document.createElement("p");
var text = document.createTextNode("Tutorix is the best e-learning platform");
tag.appendChild(text);
var element = document.getElementById("new");
element.appendChild(tag);
</script> </body> </html>
ผลลัพธ์
Tutorix Tutorialspoint Tutorix is the best e-learning platform