Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created September 17, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhei/4280c244eed206a72ab437950d62603b to your computer and use it in GitHub Desktop.
Save shuhei/4280c244eed206a72ab437950d62603b to your computer and use it in GitHub Desktop.
Count children of a DOM element
function countChildren(el) {
const children = el && el.children;
if (!children) {
return 0;
}
let count = 0;
for (let i = 0; i < children.length; i++) {
const child = children[i];
count += 1;
count += countChildren(child);
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment