Let's presume you got a list with nested child lists.
<ul>
<li></li>
<li>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li></li>
</ul>
And use document.querySelectorAll()
to make a selection:
var ul = document.querySelectorAll("ul");
How can i use the ul
collection to get the direct child elements?
ul.querySelectorAll("> li");
// Gives 'Error: An invalid or illegal string was specified'
Let's presume ul
is cached somehow (otherwise i could have done ul > li
directly).
In jQuery this works:
$("ul").find("> li");
But it doesn't in native querySelectorAll
. Any solutions?
.children("li")
instead of.find("> li")
. I'm surprised that works at all, actually. – Veator.find('> li ul span')
is useful, but not so much on its own like in the OP's example. – Buiron