I have the code below to add a .active class to a link if the href is equal to the url. It is working for the menu items but for the submenu items its not working properly. For example if the link with text "Edit" is clicked i want to add the .active class to the main link with text "Item 1". But its not working tihs part, do you know how to achieve that?
html
<nav id="nav">
<ul class="">
<li class="active"><a href="url">Item 0</a></li>
<li>
<a href="#edit" data-toggle="collapse" aria-expanded="false">
Item 1
</a>
<ul class="collapse" id="edit">
<li><a href="/create">Create</a></li>
<li><a href="/edit">Edit</a></li>
</ul>
</li>
<li><a href="/posts">Item 2</a></li>
</ul>
</nav>
js
$("#nav a").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});