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 7051157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:13:48+00:00 2026-05-28T03:13:48+00:00

I want to style a list using JavaScript to add a class to the

  • 0

I want to style a list using JavaScript to add a class to the active firstlevel list-item. So if you click ‘Flyer’, <li><a href="#">Downloads &raquo;</a> becomes <li class="active"><a href="#">Downloads &raquo;</a>.

HTML

<ul id="navmenu">
  <li><a href="#">Downloads &raquo;</a>
    <ul>
      <li><a href="downloads_flyer.php">Flyer</a></li>
      <li><a href="downloads_jahresberichte.php">Jahresberichte</a></li>
      <li><a href="downloads_banner.php">Banner</a></li>
    </ul>
  </li>
  <li><a href="presse.php">Pressecenter</a></li>
  <li><a href="kontakt.php">Kontakt</a></li>
</ul>

JAVASCRIPT (updated)

var i = 0;
var url = "<?php echo end(explode("/", $_SERVER["SCRIPT_NAME"])); ?>";
var liste = document.getElementById("navmenu").getElementsByTagName("li");
while(i < liste.length) {
    var topList = liste[i];
    var topLink = liste[i].getElementsByTagName("a")[0];
    if (topLink.href.substr((topLink.href.length - 1), 1) != "#"  // To avoid matching the 'Downloads' link.
     && topLink.href.indexOf(url) > 0)
    {
        topList.className = "active";
    }
    i++;
}

As you can see it adds ‘active’ to the LI containing the link but it should be added to the toplevel LI.
How can I get the script running correctly – what is my mistake?

Final Solution

var i = 0;
var url = "<?php echo end(explode("/", $_SERVER["SCRIPT_NAME"])); ?>";
var liste = document.getElementById("navmenu").getElementsByTagName("li");
while(i < liste.length) {
    var topLink = liste[i].getElementsByTagName("a")[0];
    if (topLink.href.substr((topLink.href.length - 1), 1) != "#"  // To avoid matching the 'Downloads' link.
     && topLink.href.indexOf(url) > 0)
    {
        if(topLink.parentNode.parentNode.parentNode.tagName == "LI") {
            topLink.parentNode.parentNode.parentNode.className = "active";
        } else {
            topLink.parentNode.className = "active";
        }
    }

    i++;
}

Thanks for your help guys!

  • 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-28T03:13:49+00:00Added an answer on May 28, 2026 at 3:13 am

    You have to increment you var i. Your while enters on an infinite loop! Intead of use document.URL, try using $_SERVER['SCRIPT_NAME']:

    var i = 0;
    var url = "<?php echo end(explode("/", $_SERVER["SCRIPT_NAME"])); ?>";
    var liste = document.getElementById("navmenu").getElementsByTagName("li");
    while(i < liste.length) {
        var topLink = liste[i].getElementsByTagName("a")[0];
        if (topLink.href.substr((topLink.href.length - 1), 1) != "#"  // To avoid matching the 'Downloads' link.
         && topLink.href.indexOf(url) > 0)
        {
            topLink.parentNode.parentNode.parentNode.className = "active";
        }
    
        i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add some style to head tag in html page using javascript.
I want to add the dropdown style date selector which repopulates the date list
I want to style the last TD in a table without using a CSS
I want to add style in ASP.NET textbox control. I couldn't find textbox element
Assume you have a list item, <li id=foo> which you want to fade from
Im using this .sortable function on my list. My problem is that I want
I want this HTML: <ol style="list-style:decimal;"> <li>Apples</li> <li>Oranges</li> </ol> to render like this: Q1.
Want to populate dynamically combobox-listbox-drop-down using javascript. var table = document.createElement(table); var select =
Is there a way to increase the size of just the bullet list-style-type using
I want to access the inner div and get it's value using Javascript. Would

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.