i’m not getting forward in this so i need your help.
It’s a triple nested menu for wordpress. And it looks like this:
<ul class="sub-menu">
<li><a href="#">Link</a></li>
<li><a href="#" class="has_children">Link with children</a>
<ul class="sub-menu">
<li><a href="#">Link</a></li>
<li><a href="#" class="has_children">Link with children</a>
<ul class="sub-menu">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</li>
</ul>
</li>
</ul>
I want to add a class has_children to each link which has a ul.sub-menu.
My code now is:
jQuery.each(jQuery('ul.sub-menu li').has('ul.sub-menu'), function() {
jQuery('a', this).addClass('has_children');
})
That works for the first ul.sub-menu li but all other child links now have the class has_children even if they don’t have a sub-menu.
EDIT [Solution]:
jQuery.each(jQuery('ul.sub-menu li').has('ul.sub-menu'), function () {
jQuery('>a', this).addClass('has_children');
})
That did the trick! 🙂 Thanks
You need to change your selector to only match the direct
awithin the currentliotherwise it will match allawithin all levels. Use>ato match the directaonly, similar to this:DEMO – Matching only the
adirectly within the currentliIn the DEMO I removed all the
has_childrenclasses you had in the initial HTML to show that indeed they are added as expected by the script.