Jquery if hasClass then addClass

css, html, javascript, jquery

Solution

Think this is what you want:

$('.nav-column li').each(function(){
    if($(this).hasClass('active')) {
        $(this).closest('.nav-column').siblings('div.home').toggleClass("off");
    } 
});

Fiddle:

http://jsfiddle.net/Jf8mp/

Your mistakes:

`.sibling('div.home')` is wrong, the correct name of method is `.siblings()`

if condition doesnt determine who is `$(this)`, you have use a function as `.each()`

UPDATED:

to make it work on hover over `.nav-column ul li a`:

$('.nav-column li').on('mouseenter','a',function(){
    if($(this).closest('li').hasClass('active')) {
        $(this).closest('.nav-column').siblings('div.home').toggleClass("off");
    } 
});

Fiddle:

http://jsfiddle.net/Jf8mp/2/

Problem

I know I can do this, I'm just getting lost in the hierarchy and need a second set of eyes on this. Here's the structure 'm working with: ``` <div class="nav-column"> <ul> <li><a href="#">Link 01</a> <div> <ul> <li><a href="#">Sublink 01</a></li> <li><a href="#">Sublink 02</a></li> <li><a href="#">Sublink 03</a></li> </ul> </div> </li> <li><a href="#">Link 02</a></li> <li><a href="#">Link 03</a></li> </ul> <div class="home"><h3>Underlying Div</h3></div> </div> ``` I am looking to do the following: when you hover over a `.nav-column ul li a` that visibility of `div.home` would turn off. Of course there are going to be multiple .nav-columns so I'm making this dynamic. The jQuery I have right now is: ``` if ($('.nav-column li').hasClass('active')){ $(this).parent('.nav-column').sibling('div.home').toggleClass("off"); } ``` without yielding any class addition to the div.home. I already have a hover function adding and removing the class `'.active'` to the `.nav-column li` EDIT EDIT EDIT I see that I have made a mistake with my code, and in fact the correct code has the `div.home` OUTSIDE the `div.nav-column` This is the proper heirarchy: ``` <div class="wrapper"> <div class="nav-column"> <ul> <li><a href="#">Link 01</a> <div> <ul> <li><a href="#">Sublink 01</a></li> <li><a href="#">Sublink 02</a></li> <li><a href="#">Sublink 03</a></li> </ul> </div> </li> <li><a href="#">Link 02</a></li> <li><a href="#">Link 03</a></li> </ul> </div> <div class="home"><h3>Underlying Div</h3></div> </div> ``` Once again... I am very sorry... you can sense my sanity levels dropping

Original source