3

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

2
  • But, div.home is not a sibling of .nav-column though it's hard to see that as you haven't indented your code correctly Commented May 22, 2014 at 14:35
  • put the complete jQuery function because we dont know who is $(this) Commented May 22, 2014 at 15:11

5 Answers 5

8

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/

Sign up to request clarification or add additional context in comments.

3 Comments

I think this is almost there... I can't get this to work when I hover over Link 01. The active state works fine.. but nothing else.
Updated. Look if is it what you need.
I had to modify the on function to a hover and change the toggle to an addClass and append a removeClass... but this worked perfectly...
5

You need to use .parents() instead of .parent(). .parents() traverses up multiple levels of the DOM while .parent() retrieves the immediate parent of the element matching the selector.

$(this).parents('.nav-column').siblings('div.home').toggleClass("off");

Since you're not targetting the direct parent (ul) of the element matching the selector, you'll need to use .parents() instead.

In addition, you have a second problem. According to your code structure div.home is not a sibling of .nav-column. You can use .find() for this instead.

Per your last edit, the previous statement is no longer applicable. The code snippets have been updated to reflect your edited change.

Alternatively, you could do the following to accomplish the same effect:

$(this).parents('.nav-column').next().toggleClass("off");

Comments

2

use .closest() or .parents() instead of .parent(). also div home is not sibling of .nav-column. You need to use .find() instead of .siblings().Try this:

 $(this).closest('.nav-column').find('div.home').toggleClass("off");

or

 $(this).parents('.nav-column').find('div.home').toggleClass("off");

Comments

1

the .parent() only goes up one node, you need to use .parents('select')

Comments

0

If I understand:

$('.nav-column li').forEach(elem, index){
    elem = $(elem);
    if (elem.hasClass('active')){
       elem.addClass("Cheese");
    }
}

This would add the class "Cheese" to any active li :)

1 Comment

Looks like I didn't. May still help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.