How to get all nested JS elements?

  • 0
    let's say
    <div>
    	<div>
    		<div>
    		</div>
    	</div>	
    </div>

    I want to get all three elements, while knowing the nesting level of each
    JavaScript Jaxon Shaffer, Aug 10, 2019

  • 2 Answers
  • 0
    const getElementsWithDepth = (el, level = 0) =>
    [...el.children].reduce((acc, n) => {
    acc.push(...getElementsWithDepth(n, level + 1));
    return acc;
    }, [ { el, level } ]);
    Anonymous

  • 0
    Node.childNodes returns a collection of children for this element.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!