jquery - add class to parent li if Child element is actve -


i have searched on can't seem figure how this. have menu has sub navigation in. want able highlight parent li when child li selected. have figured out how child selected not parent. please help.

javascript

$('.nav a').each(function (index) {     if (this.href.trim() == window.location) {         $(this).addclass("selected");     } }); 

html

<ul class="nav">     <li><a id="a2" href="~/" runat="server">home</a></li>     <li><a href="/about.aspx">about us</a></li>     <li class="dropdown"><a href="#contact" role="button" class="dropdown-toggle" data-toggle="dropdown">products</a>         <ul class="dropdown-menu" role="menu">             <li><a href="/products-access-control.aspx">product 1</a></li>             <li><a href="/products-time-attendance.aspx">product 2</a></li>             <li><a href="/products-service.aspx">product 3</a></li>             <li><a href="/partner-solutions.aspx">product 4</a></li>         </ul>     </li>     <li><a href="#" id="newslink1" runat="server">news</a></li>     <li><a href="/contact.aspx">contact us</a></li> </ul> 

when set selected class on $(this) can $(this).parent().addclass("selected"). parent() can called again , again go tree (li -> ul -> li etc).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -