9951 explained code solutions for 126 technologies

javascriptGet all children and grandchildren of an html element recursively

const getAllChildren = (htmlElement) => {
  if (htmlElement.children.length === 0) return [htmlElement];

  let allChildElements = [];

  for (let i = 0; i < htmlElement.children.length; i++) {
    let children = getAllChildren(htmlElement.children[i]);
    if (children) allChildElements.push(...children);

  return allChildElements;
};ctrl + c
htmlElement.children.length === 0

check if we are at the deepest element. If there is no child, we are.

allChildElements = []

all child elements for current htmlElement.


push incoming children from deeper level of recursive call.


push the element itself at the end of iteration.