I have searched a lot for adding active class to the parent menu using javascript.
I found many more examples but not a single one is working for me, below is my code
HTML
<div id="menu1" class="hmenu">
<ul>
<li><a href="#">Item1</a>
<ul>
<li><a href="#">SubItem1</a>
<ul>
<li><a href="#">SubSubItem1</a></li>
<li><a href="#">SubSubItem2</a></li>
</ul>
</li>
<li><a href="#">SubItem2</a> </li>
<li><a href="#">SubItem3</a>
<ul>
<li><a href="#">SubSubItem1</a></li>
<li><a href="#">SubSubItem2</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Item2</a></li>
<li><a href="#">Item3</a>
<ul>
<li><a href="#">SubItem1</a>
<ul>
<li><a href="#">SubSubItem1</a></li>
<li><a href="#">SubSubItem2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<br style="clear: left" />
</div>
My requirement is when i click on SubItem1 then both Item1 and SubItem1 should be active.
And when i click on SubSubItem1 then SubSubItem1 ,SubItem1 and Item1 should be active.
Means when click on any link then its all parent link and the same link should be active.
I have tried with this javascript code :
$(document).ready(function () {
$('.hmenu ul li ul').find('li').click(function () {
//removing the previous selected menu state
$('.hmenu').find('li.active').removeClass('active');
//adding the state for this parent menu
$(this).parents('li').addClass('active');
});
});
Actually i don't have any experience with javascript coding and unable to figure out the problem in my code.
Can anyone suggest me for the same.
.hmenu ul li ul').find('li')you aren't getting al yourlis.