Using the following sample markup:
<ul>
<li><div>Hello</div>World</li>
<li>Hello World
<ul>
<li style="voice-family: dizzy">What a wonderful world</li>
</ul>
</li>
<li><a href="hello.html">Hello World</a>
<ul>
<li>Goodbye world</li>
</ul>
</li>
</ul>
I'm trying to create a jquery selector that will grab the first element of each top level LI that is NOT a div, but COULD be either another DOM element OR a text string.
So, running it against the above, I'd like to return 3 elements:
- World
- Hello World
<a href="hello.html">Hello World</a>
Assuming I already created a selector to grab each of the top level LIs (We'll call that $topLevelLIs) I've come up with this:
$topLevelLIs.find("*:first:not(div)")
Alas, as you can probably see, that will only return a dom element, and, as such, will only find something in option #3. The first two are just returned as empty.
Is there a way to do a "Grab the first DOM element or grab the first text string before the next DOM element" logic?
SOLUTION:
Thanks to the help of Russ and Marc I ended up using this to eventually get the type of node, and then I can act upon that:
$topLevelLIs.contents(":not(div)")
.filter(function(index) {
return index == 0
}).get(0).nodeType