JS DOM: Get Parent, Child, Sibling
Get Parent
node.parentElement-
Value is the parent of node, or
nullif none.
Get Sibling
element.previousElementSibling-
Value is the previous sibling, or
nullif none. element.nextElementSibling-
Value is the next sibling or
nullif none.document.getElementsByTagName("p")[0].nextElementSibling.style.color = "green";
Get First/Last Child
element.firstElementChild-
Value is the first child, or
nullif none. element.lastElementChild-
Value is the last child, or
nullif none.
Get All Children
element.children-
Value is the element's children.
result is a Live Object and HTMLCollection.
const xx = document.body; for (let x of xx.children) x.style.color = "green";
Include Whitespace Nodes
The following may return Whitespace Nodes .
node.parentNodenode.previousSiblingnode.nextSiblingnode.firstChildnode.lastChildnode.childNodes-
node.hasChildNodes() -
Return
trueif it has children, including Whitespace Nodes. Elsefalse.document.getElementById("xd1d13").hasChildNodes();