0

I am trying to add a class dynamicly to the menu I have on my website, which is build up as followed.

<nav>
    <ul id="" class="mini-menu">
        <li><a href="home">Home</a></li>
        <li><a href="over-jellyfish">Over Jellyfish</a></li>
        <li><a href="blog">Blog</a></li>
        <li class="contact"><a href="contact">Contact</a></li>
    </ul>
    <ul class="hoofd-menu">
        <li class="websites"><a id="dekstop_menu" href="websites"><i class="fa fa-desktop text-center"></i>Websites</a></li>
        <li class="support"><a id="support_menu" href="support"><i class="fa fa-comment-o text-center"></i>Support</a></li>
    </ul>
</nav>

with Jquery I am tryint to add the class active to the mini-menu by doing the following steps.

jQuery(function() {
    var str = window.location.pathname;
    var page = str.split("/");
    p=page[2];
    var active = p=page[2];
    console.log(active);  

    jQuery('.mini-menu a').each(function() {
        if (jQuery(this).attr('href')  ===  active) {
            jQuery(this).addClass('active');
        }
    });
}); 

Yet i seem to miss something here, cause nothing shows up within the html as adding a class to the link. Am I missing the fact that the link is not a direct child of the class mini-nav? If so, how do I fix this?

7
  • Can you create jsfiddle and explain your issue? Commented Oct 14, 2014 at 13:04
  • excuse me, typo .. it's mini-menu .. fixed it Commented Oct 14, 2014 at 13:05
  • Does the console.log() return the correct string? Commented Oct 14, 2014 at 13:07
  • No maybe it's phrased wrong, i am trying to check if the variable active equals to the href link within the anchor tag. if that is true, addclass "active" Commented Oct 14, 2014 at 13:10
  • 1
    It should work fine, btw you don't need to use .each() to find the link element with the correct href. You can use attribute selector jQuery('.mini-menu a[href="'+active+'"]').addClass('active'); Commented Oct 14, 2014 at 13:14

1 Answer 1

1

Can you try this :

jQuery(function() {
    var str = window.location.pathname;
    var page = str.split("/");
    p=page[2];
    var active = p=page[2];
    console.log(active);  

    jQuery('.mini-menu a').each(function() {
        (function (self) {
        if (jQuery(self).attr('href')  ===  active) {
            jQuery(self).addClass('active');
        }
        })(this);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That was what I was looking for. I adds the class now :) I ll accept when I can.
It's because of "each". When you are using "each" and that you would like to change something : create a function like I did to copy the current variables inside. If you don't do that, the "this" will always be equal to the last element when you will try to change something.

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.