Am having a menu like this
<li> <a href="/home" class="active">Home</a> </li>
<li> <a href="/service">Services</a></li>
I want the li in which the link has active have the class “active”. So it will become
<li class="active"> <a href="/home" class="active">Home</a> </li>
I think code can be something like this, but not quite getting it though
var $this;
$("ul > li").each(function(){
$this = $(this);
if($this.find("> li a.active").length)
$this.addClass("active");
}
Why don’t you find the element whose class is “active” and get its parent and add class. This could be as simple as: