2

Trying to mix a variable with :not then use that variable with addClass. but no luck yet.

HTML

<ul>
  <li><a href="#">Nav One</a></li>
  <li><a href="#">Nav Two</a></li>
  <li><a href="#">Nav Three</a></li>
  <li><a href="#">Nav Four</a></li>
</ul>

LESS

ul{
    li{
        a{
            color: black;
            transition: all 0.5s ease;
            font-size: 3rem;
            text-decoration: none;
        }
    }
}
.enc-non-active{
    color: gray;
}

JAVASCRIPT

myFunction('ul li a', 'enc-active', 'enc-non-active');

function myFunction($menu, $activeClass, $nonActiveClass) {
   $(function(){
        $($menu).hover(function(){
            $(this).addClass($activeClass);
        },
        function(){
            $(this).removeClass($activeClass);
        });
        $($menu).hover(function(){
            var fullPath = $menu + ':not(' + $activeClass + ')';
            $(fullPath).addClass($nonActiveClass);
        },
        function(){
            var fullPath = $menu + ':not(' + $activeClass + ')';
            $(fullPath).removeClass($nonActiveClass);
        });
    });
}

Here is the CodePen http://codepen.io/antfuentes87/pen/pJEaeb if anyone can let me know what I am doing wrong, that would be great. Thanks for your time.

5
  • 1
    What's the value of $menu and $activeClass? Commented May 22, 2015 at 1:29
  • Whatever the person wanted using the function. For example I would call it like this myFunction('ul li a', 'enc-active', 'enc-non-active'). Did you look at the codepen? Commented May 22, 2015 at 1:30
  • 1
    What function? Please post the code here, not just at CodePen. Commented May 22, 2015 at 1:30
  • There's no need to use CodePen at all, now that SO has Stack Snippets. Commented May 22, 2015 at 1:31
  • Alright, I will check into the Stack Snippets. For now I just put the code in the post. Commented May 22, 2015 at 1:34

2 Answers 2

2

You forgot a dot in :not(. - easily fixed http://codepen.io/anon/pen/zGKRyW

var fullPath = $menu + ':not(.' + $activeClass + ')';
$(fullPath).addClass($nonActiveClass);
Sign up to request clarification or add additional context in comments.

Comments

1

You are missing a dot to make the class name into a css selector

Change

var fullPath = $menu + ':not(' + $activeClass + ')';

To

var fullPath = $menu + ':not(.' + $activeClass + ')';

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.