Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between document.append() and document.appendchild()?

I was watching a javascript tutorial.In that he told about document.append() and when i checked w3schools.It showed same function as document.appendchild()..What is the difference?

What I have tried:

Both gave same result.

const newDiv = document.createElement("div");
 html.append(newDiv);

 const newDiv2 = document.createElement("div");

html.appendChild(newDiv2)
Posted
Updated 12-Dec-21 10:06am

1 solution

append() accepts either a node or a string value. This you might want to use if you want to append some text as a child.

appendChild() accepts only a node, so you would need to create a DOM element before using it.

JavaScript
const container = document.getElementById('container');
const child1 = document.createElement('b');
child1.append('Bold');

const child2 = document.createElement('u');
child2.append('Underline');

container.append('Normal!');       // ok
container.append(child1);          // ok
container.appendChild(child2);     // ok
container.appendChild('And more'); // error
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900