Create detached elements
Quick answer
document.createElement creates an element that is not yet attached to the document. Set its properties and insert it when ready.
Browser example
Run in a browser document, not a bare Node.js process.
const item=document.createElement("li");item.textContent="Book";const list=document.createElement("ul");list.append(item);console.log(list.firstElementChild.textContent,item.isConnected);Expected console output:
Book falseCommon mistake
A node inside a detached subtree is not connected to the document.
Practice
Build a labeled button without parsing markup.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Create detached elements — Knowledge Check
Check the central concept, apply it to a new scenario, and predict browser behavior.
Does creating an element automatically insert it into the document?
Where can you configure the label of a newly created button before inserting it?
A detached list contains a newly created item. What does item.isConnected report?
How do you make a new paragraph a child of a connected container?
What is printed by this browser snippet? (Create detached elements)
const p=document.createElement("p");console.log(p.isConnected);document.body.append(p);console.log(p.isConnected);p.remove();