I have a menu with 2 submenus. I want to target the last li a
element in each menu at once and I would like it to be dynamic so if another submenu is added it would find and target its last li a
element.
I would like to do with without added any more classes or id's than what is used already. I have tried just targeting the class .sub-menu
but it only selects the last sub-menu. Is there a way to select all occurrences?
<ul id="menu-main" class="menu">
<li class="menu-item-has-children"><a href="#">About</a>
<ul class="sub-menu">
<li><a href="#">Our History</a></li>
<li><a href="#">Strategic Plan</a></li>
<li><a href="#">Board of Directors</a></li>
</ul>
</li>
<li class="menu-item-has-children"><a href="#">Programs</a>
<ul class="sub-menu">
<li><a href="#">Monthly Calendars</a></li>
<li><a href="#">Early Years</a></li>
<li><a href="#">Children and Youth</a></li>
</ul>
</li>
</ul>
Right now I am trying
$('li.menu-item-has-children:nth-child(1) li').last().focusout(function() {
$(this).parent().parent().removeClass('hover');
console.log("T removed");
});
$('li.menu-item-has-children:nth-child(2) li').last().focusout(function() {
$(this).parent().parent().removeClass('hover');
console.log("T2 removed");
});
Should I make a function that does this for me? I am thinking there must be a easier way.
Thanks, any help is greatly appreciated.