Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:40:16+00:00 2026-06-04T18:40:16+00:00

I hope to find help here, I am stuck. I use this function to

  • 0

I hope to find help here, I am stuck.

I use this function to get the exact active li element in a menu (with nth-child):

function getElementPathWithJquery(element) {
    return $(element).parents().andSelf().map(function() {
        var $this = $(this);
        var entry = '';        
        if(this.nodeName === "BODY" || this.nodeName === "HTML") {
        // do nothing
        } else {
        entry += this.nodeName;
        if (this.className) entry += "." + this.className.replace(/ /g, '.');
        if (this.id) entry += "#" + this.id;
        if ($this.siblings(entry).length > 0) entry += ":nth-child(" + $this.prevAll(entry).length + ")";
        }
        return entry;
    }).get().join(" ");
}

Now, being at the requested level, the parent li-element shall get a addClass(‘active’) attribute, but the following does not work:

if( $('#MENU_MAIN').find("ul.menu-level-3>li").hasClass('active')) {
var path = getElementPathWithJquery($('#MENU_MAIN').find("ul.menu-level-3>li.active"));
$(path).parent().parent().css('border','3px solid red');
}

The reason why I need the exact nth element is the fact that the menu system is a category system an one entry can actually belong to more categories. a solution like:

$('#MENU_MAIN').find("ul.menu-level-3>li.active").parent().parent().css('border','3px solid red');

works, but depending on the number of categories the found entry belongs to, more than just one parent li could have a red border.

Thanks for your help!
Regards,
Robert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:40:17+00:00Added an answer on June 4, 2026 at 6:40 pm

    You can use parents() to get all element’s ancestors; you can use a selector to limit the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The last minimized code is, I hope it will help someone: $(#menu).find(a).hover(function(){ $(this).addClass(active); },function(){
I am stuck here and hope to get some guidance on this. I'm trying
I hope I will find a help here. I have a quite complex MVC
I hope someone can help me with this, as I'm unable to find the
Hope find a guru's help to figure out the next problem. I have two
Hope somebody could help me out, I am trying to find a solution for
This is a challenge question / problem. Hope you find it interesing. Scenario: You
I hope this question is not a duplicate but I didn't find anything equivalent
I have been trying to find this issue here, but no topic seems to
I am new to rails so could use some help here. I have followed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.