Created
September 17, 2018 16:45
-
-
Save shuhei/4280c244eed206a72ab437950d62603b to your computer and use it in GitHub Desktop.
Count children of a DOM element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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