I'm developing a small application that has a tree structure of the menu, so this html:
<ul class="sidebar-menu"> <li class="active"><a href="javascript:;"> ELEMENT 1 </a></li> <li class="treeview"> <a href="javascript:;"> ELEMENT 2 <span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a> <ul class="treeview-menu"> <li><a href="javascript:;"> ELEMENT 2.1 </a></li> <li><a href="javascript:;"> ELEMENT 2.2 </a></li> </ul> </li> <li><a href="javascript:;"> ELEMENT 3 </a></li> </ul>
I am trying to highlight the item clicked li, adding to it a class .active . But when you click on a sub-element, I also want to highlight the parent. For example, if you press ELEMENT 2.1 , I also want to highlight the ELEMENT 2 .
This is my jQuery code to get first item with a click, and then its parent limit (I can not do without ir selector :not for other reasons:
$(".sidebar-menu").on("click", "li:not(.treeview)", function(e){ $(e.delegateTarget).parents("li").first(); } ) on ( "click", "li: not (.treeview)"., function (e) { $(".sidebar-menu").on("click", "li:not(.treeview)", function(e){ $(e.delegateTarget).parents("li").first(); } ") first ().; $(".sidebar-menu").on("click", "li:not(.treeview)", function(e){ $(e.delegateTarget).parents("li").first(); }
The if statement works as expected, but the second line always gives me an error undefined in the console.
source share