Interactive two-tier horizontal navigation menu emphasizing active page

Having created a two-tiered menu using CSS and HTML with 'selected' and 'active' classes, I am facing an issue where the second-level submenu is not displaying correctly on the current page.

The primary objective is to have the top-level menu item selected with the 'selected' class, while the corresponding item in the sub-menu should be bold ('active'). For instance, when navigating to '/maintenance/company.aspx', the "Maintenance" menu item should be selected and the "Companies" sub-menu item should appear bold.

The selection of the first level works fine, as does making the text bold. However, the actual display of the submenu (second level) is not functioning properly.

CSS

#menu
{
    margin:0;
    padding:0;
    list-style:none;
    position:relative;
    background:url(../images/menubg.gif) repeat-x;
    height:32px;
    text-transform:uppercase;
    font-family: Arial, Helvetica, sans-serif;
}
#menu li
{
    float:left;
    padding:0;
    margin:0;
}
#menu a
{
    text-decoration:none;    
    padding:10px 15px;
    display:block;
    color:#ffffff;
}
#menu li:hover a, a.selected
{
    background:#ffffff;
    color: #666666 !important;
}
#menu li span
{
    float:left;
    padding: 5px 0;
    position:absolute;
    display:none;
    left:0;
    top:31px;
    background:#ffffff;
    color: #000;
    width: 100%;
}
#menu li:hover span, .selectedSubMenu { display:block; }
#menu li span a { display: inline; padding: 5px 15px; } 
#menu li span a:hover {text-decoration: underline;}
.active { font-weight: bold; }

HTML

<ul id="menu">            
    <li><a href="#" id="contracts">Contracts</a>
        <span>
            <a href="/contracts/submitcontract.aspx">New Contract</a>
            <a href="/contracts/search.aspx">Find Contract</a>
        </span>
    </li>
    <li><a href="#" id="invoicing">Invoices</a>
        <span>
            <a href="/invoicing/invoicerun.aspx">Invoices Run</a>
            <a href="/invoicing/invoicerun.aspx">Invoicing History</a>
        </span>
    </li>
    <li><a href="#" id="maintenance">Maintenance</a>
        <span>
            <a href="/maintenance/account.aspx">Accounts</a>
            <a href="/maintenance/agent/search.aspx">Agents</a>
            <a href="/maintenance/company.aspx">Companies</a>
            <a href="/maintenance/contact.aspx">Contacts</a>
            <a href="/maintenance/networks.aspx">Networks</a>
            <a href="/maintenance/plans.aspx">Plans</a>
        </span>
    </li>
    <li><a href="#" id="admin">Control Panel</a>
        <span>
            <a href="/admin/userlist.aspx">Users</a>
            <a href="/admin/systemsettings.aspx">System Settings</a>
        </span>
    </li>
</ul>

JQUERY

function markActiveLink() {
    var path = location.pathname.substring(1);

    if (path) {
        $('#menu li > a').each(function() {
            var $category = $(this).attr('id');
            if (path.indexOf($category) > -1) {
                $(this).addClass('selected');

                $(this).siblings('span:first').addClass('selectedSubMenu');

                $('#menu li span a[@href="/' + path + '"]').addClass('active');                        
            }
        });              
    }
}

$(document).ready(markActiveLink);

Answer №1

After some thorough investigation using FireBug, I was able to identify the issue and resolved it by adding more specificity to my CSS code.

#menu li:hover span, .selectedSubMenu { display:block; }

was updated to

#menu li:hover span, #menu li span.selectedSubMenu { display:block; }

Furthermore, I made adjustments to change the color of the anchor element with the following CSS:

#menu li span.selectedSubMenu a { color:#666666; }

I found useful information on which helped clarify the concept for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for successfully passing three parameters to the controller

This is my controller, designed to accept a date: public ActionResult ViewLevel(string datepicker) { @ViewBag.DatePicker = datepicker; return View(); } Here is my View: @model Car @{ var datepicker = Convert. ...

Utilize JSoup to scrape and extract all textual hyperlinks

Currently, I am utilizing JSoup for extracting content from various web pages. My primary goal is to extract all the links on a page that contain some text within them. The text itself does not have specific requirements as long as it is non-empty and not ...

The utilization of ng-class in a dropdown menu: A step-by-step guide

I am new to AngularJS and I am trying to figure out how to disable a selectlist when an option has been chosen and enable it when no option has been selected. My goal is to have the selectlist disabled if an object is not empty, and enabled if it's em ...

Encrypting user search queries on the client-side before navigating to the search results page

Currently, I am in the process of updating an application's user interface, specifically focusing on moving the search box functionality to the client side. The current setup involves a search button triggering a server-side method upon click, which e ...

Exporting a single xls sheet with multiple tables can be done using Alasql

Currently utilizing alasql for exporting to an xls file. Successful in exporting a HTML table to xls, however facing the challenge of having multiple tables within my HTML and seeking to export to xls with only one sheet containing all tables; similar to t ...

What is the best way to center a div vertically inside another div that contains text?

I am attempting to vertically center a div within a parent div that contains text. Here is my current setup: The alignment appears a bit off, as the text seems centered properly while the yellow boxes are not. This is how I am currently approaching it: . ...

Are you looking to work with an ASMX web service and navigate JSON utilizing JavaScript or

Currently, I am using asmx to fetch data from a database. public class TestPage1 { public int UserID { get; set; } public string UserName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public ...

Can an object serve as a property name for another object?

Can this be achieved using JavaScript? I'm attempting to assign the property name of an object as a "HTMLInputElement": var el = $('#something').get(0), obj = {}; obj[el] = 'some random data'; Unfortunately, it ...

Bootstrap carousel slide animation isn't transitioning properly

I am encountering an issue with my carousel animation. I am aiming for a slide-out/slide-in effect similar to the Bootstrap website, but instead of sliding out, the old item simply disappears and the new item slides in. I have included a GIF to illustrat ...

Something seems to be preventing Div from appearing and there are no error messages appearing

I've been attempting to create a menu, but the toggle div for the menu isn't visible Following a tutorial where an individual sets up a menu, there is a div with a menu icon in the top left corner. Despite meticulously copying the code from the ...

The start-up process of AngularJS applications with Bootstrap

Curious about the predictability of the initialization flow in AngularJS apps? Want to understand the order of execution of different blocks within an HTML document? I came across a question on bootstrapping in Angular JS, but it didn't delve into th ...

Transmitting JSON data using a Jquery AJAX request in PHP

I'm facing an issue passing a JSON array from a jQuery AJAX call to a PHP file and attempting to write the received data to a file. This is my code: var contacts = [{"address":[],"phone":[],"last_name":"abc","email":[{"address":"<a href="/cdn-cgi/ ...

What is the best way to execute PHP code based on a checkbox or slider input?

This is my first time seeking help on this platform, so I hope this question is appropriate. Despite my efforts to search online and watch tutorials, I'm struggling to make my code work as I am still new to web development. Essentially, I've cre ...

Is there a way to maintain consistent height among vertical divs?

Is it possible to create a ranking system that adjusts based on the user's points while maintaining consistent height with an image? How can I eliminate the br tags in this scenario? <!DOCTYPE html> <html lang="en>" <head> <m ...

Firefox does not display the line headings on the sides

Hey everyone, I'm facing a compatibility issue with CSS and Firefox today. When I go to my website www.fashoop.com, you'll notice some header lines on the sides. Here's an example in Google Chrome: (GOOGLE CHROME EXAMPLE) COLOR BLUE AN ...

Tips for extracting information from a JSON file using $routeParams in AngularJS

https://i.stack.imgur.com/NQCpy.png I am currently encountering an issue with retrieving data using $routeparams. Here is a description of my problem: Retrieving the URL to redirect console.log($routeParams); console.log($routeParams.json_url); $.getJS ...

The completion event for Ajax and Json does not activate Google Maps

I'm currently facing an issue with my Google Maps functionality. Despite trying various solutions that involve hidden tabs, the problem persists. The trouble lies in the fact that my ajax function successfully posts and retrieves data, but fails to tr ...

How to Incorporate Multiple React Components in an HTML File

Attempting to integrate React code into an HTML file has been a challenging process for me. I've been following the official React documentation and successfully implemented their basic like button starter code on my page. Initially, everything worked ...

Integrating Third-Party Scripts into a Single Magento Page

Despite my efforts to research, I still feel like I'm missing a key piece of information. On my photo gallery page, I am utilizing fancybox to display larger versions of the images when clicked. To make this work, I need to include two scripts for fa ...

Properly connecting stylesheets and JavaScript files in EJS: A guide

I've encountered a problem while using ejs to link external stylesheets and script tags. Here's the link for my stylesheet: <link rel="stylesheet" href="styles/index.css"> When I click on the link to the sheet and follo ...