I'm attempting to target specific top level list items with jQuery. However some of these list items also include nested lists of their own,which I want to ignore.
I found a stackoverflow question here Target first level <li>s and not the nested <li>s about this very topic but after enacting the solutions I'm still encountering the same issue.
HTML:
<div>
<ul>
<li data-level="1">
Upper List Item
<ul>
<li>Sub list item</li>
</ul>
</li>
<li data-level="1">List item 2</li>
</ul>
</div>
Javascript:
$(document).ready(function() {
$("div > ul > li[data-level=1]").each(function(index){
console.log(index + ":" + $(this).text());
});
});