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

  • Home
  • SEARCH
  • 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 6130177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:49:39+00:00 2026-05-23T16:49:39+00:00

Is there an easy way to add a bit about also setting the ancestor

  • 0

Is there an easy way to add a bit about also setting the ancestor li tags to .active?

<script language="javascript" type="text/javascript">
function setActive() {
  aObj = document.getElementById('toc').getElementsByTagName('a');
  for(i=0;i<aObj.length;i++) { 
    if(document.location.href.indexOf(aObj[i].href)>=0) {
      aObj[i].className='active';
    }
  }
}
window.onload = setActive;
</script>
  • 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-05-23T16:49:39+00:00Added an answer on May 23, 2026 at 4:49 pm

    It depends on what you mean. I don’t get a full picture of what’s going on from your question. From the looks of it you’re grabbing an element with the id of “toc”, so I’m guessing this toc contains LI elements as well as A elements. In which case you could use an asterisk in getElementsByTagName, which could be slower depending on how many other elements are there.

    function setActive() {
      var href = window.location + ''
        , el = document.getElementById('toc')
        , a = el.getElementsByTagName('*')
        , i = 0
        , l = a.length
        , name;
    
      for (; i < l; i++) { 
        el = a[i];
        name = el.nodeName.toLowerCase();
        if (name === 'li' || (name === 'a' 
            && ~href.indexOf(el.href))) {
          el.className = 'active';
        }
      }
    }
    

    If you’re asking for ancestral LI higher up in the document tree, this is what
    you would want:

    function setActive() {
      var href = window.location + ''
        , el = document.getElementById('toc')
        , a = el.getElementsByTagName('a')
        , i = 0
        , l = a.length;
    
      // descendants
      for (; i < l; i++) { 
        if (~href.indexOf(a[i].href)) {
          a[i].className = 'active';
        }
      }
    
      // parents
      while (el = el.parentNode) {
        if (el.nodeName.toLowerCase() === 'li') {
          el.className = 'active';
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to add shadows in opengl-es 1.x? Or only in
Is there any easy way to add a using statement to every class I
Is there an easy way to add nodes to a WinForms .NET TreeView control
Is there an easy way to add a custom RFC822 header to a message
Is there an easy way to add line number information to created nodes in
Just wondering if there is an easy way to add the functionality to duplicate
All I want to know is if there's an easy way to add lines
Is there some easy way to add event-calendar plugin style dates to my own
Is there an easy way to add padding between the checkbox in a CheckBox
Is there easy way to send email notification with cruise control? I Was reading

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.