I mentioned that you can use the
.childNodes property to get direct descendant elements.
David Hund pointed out on Twitter that you can also get direct descendants that match a specific selector (or set of selectors) by using the
> selector in
var directDescendants = document.querySelectorAll('#some-element > .pick-me');
🚀 I just relaunched my Vanilla JS Pocket Guides with new code examples and real projects to help tie everything you’ll learn together. Check it out.
Have any questions or comments about this post? Email me at firstname.lastname@example.org or contact me on Twitter at @ChrisFerdinandi.
Get Daily Developer Tips