Which method is used to remove a child node from its parent in the DOM?
Analysis & Theory
`removeChild(childNode)` is called on the parent to remove the specified child node.
Which method removes an element directly from the DOM in modern browsers?
Analysis & Theory
`element.remove()` directly removes the element from the DOM (modern browsers only).
What happens if you call `removeChild()` on a node that is not a child?
Analysis & Theory
`removeChild()` throws an error if the specified node is not a child of the parent.
What is required before calling `removeChild()`?
A
The node must be focused
B
You must select the node and its parent
C
The node must have a style
D
The node must be hidden
Analysis & Theory
To use `removeChild()`, you must have a reference to both the parent and the child node.
How do you safely remove an element by ID from the DOM?
A
document.getElementById('item').delete();
B
document.removeElementById('item');
C
document.getElementById('item').remove();
D
document.getElement('item').removeNode();
Analysis & Theory
`document.getElementById('item').remove()` is the standard way to remove an element directly.
How do you remove all children of a DOM element?
A
element.clearChildren()
Analysis & Theory
Setting `element.innerHTML = ''` removes all children inside the element.
Which method can be used to conditionally remove an element only if it exists?
A
if (element) element.remove();
B
element.deleteIfPresent();
Analysis & Theory
Use an `if` statement to check if the element exists before calling `remove()` to avoid errors.
What does `parentNode.removeChild(parentNode.firstChild)` do?
C
Removes the first child node
D
Removes the parent itself
Analysis & Theory
`firstChild` targets the first child node of the parent, and `removeChild()` removes it.
Which of the following removes only element children but leaves text nodes?
A
Looping `element.removeChild(child)` for all children
D
Looping through `element.children` and calling `remove()`
Analysis & Theory
Looping through `element.children` and calling `remove()` only removes element children, not text or comment nodes.
Which method should you avoid if you want to preserve event listeners on a node?
C
element.parentNode.removeChild(element)
Analysis & Theory
Setting `innerHTML = ''` removes all child nodes and their associated event listeners.