I want all parent items in a three level drop down menu to highlight when a child item is selected. From what I've read and been advised, this should work:
$(this).parents().addClass("selected");
But for me it does not.
I have tried both:
$(this).parent().addClass("selected");
and
$(this).parents("li").addClass("selected");
But still nothing. Can anybody see what I am doing wrong here please? You can see it in action here http://portergroup.businesscatalyst.com/hyundai.html
<!-- menu starts here -->
<div class="navBox">
<ul>
<li><a href="#">SALES</a>
<ul id="subnavlist" class="newWidth">
<li><a href="#">NEW</a>
<ul id="sub-subnavlist">
<li><a href="#">Hyundai</a></li>
<li><a href="#">Bomag</a></li>
</ul>
</li>
<li class="subNavIndustry"><a href="#">INDUSTRIES SERVED</a>
<ul class="navIndustries" id="sub-subnavlist">
<li><a href="#">Quarry and Mining</a></li>
<li><a href="#">Construction</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- menu ends here -->
$('.navBox li a').each(function(index) {
if(this.href.trim() == window.location)
//$(this).addClass("selected"); //this line works as far as highlighting the selected item
$(this).parents().addClass("selected"); //this does not work
});
Use parentsUntil(here you will put until which parent you want), for example if you want all parents until class container - you do