Jquery Enhancement for Navigation

Recently, I have implemented a header and footer navigation on my website. The header navigation consists of 1 UL (unordered list), while the footer navigation comprises 5 ULs. My goal is to align the first child of each UL in the footer navigation with the corresponding item in the header nav. The footer nav shares similar main items as the header nav but includes sub-items within them. Can someone provide assistance with this task?

To better illustrate my issue, I have created a jsFiddle:

http://jsfiddle.net/WkZuv/41/

$(".Nav > li").live({
    mouseover:function(){
        $(this).addClass("menuHover");
    },
    mouseout:function(){
        $(this).removeClass("menuHover");
    },
    click:function(){
        $(".Nav > li").removeClass("menuClicked");
        $(this).addClass("menuClicked");
    }
});

$(".footer > li:first-child").live({
    mouseover:function(){
        $(this).addClass("menuHover");
    },
    mouseout:function(){
        $(this).removeClass("menuHover");
    },
    click:function(){
        $(".Nav > li").removeClass("menuClicked");
        $(".Nav > li").eq($(this).index()).addClass("menuClicked");     

         $(".footer > li").removeClass("menuClicked");
        $(this).addClass("menuClicked");  
    }
});

.menuHover{
    background-color:#666;
    color:#fff;
}
.menuClicked{
    background-color:yellow;
    color:#666;
}

.footerTitle{
    font-weight: bold;
    color:black;
}

<h1>Header NAV</h1>
<ul class="Nav">
    <li class ="menuClicked"> List 1 </li> 
    <li>List 2 </li>
    <li> List 3 </li>
    <li> List 4 </li>
    <li> List 5</li>
</ul>

<hr />

<h1>FOOTER NAV</h1>
<ul class="footer">
    <li class ="footerTitle"> List 1 </li> 
    <li>Sub List 2 </li>
    <li>Sub List 3 </li>
    <li> Sub List 4 </li>
    <li>Sub List 5</li>
</ul>
<ul class="footer">
    <li class ="footerTitle"> List 2 </li> 
    <li>Sub List 2 </li>
    <li>Sub List 3 </li>
    <li> Sub List 4 </li>
    <li>Sub List 5</li>
</ul>
<ul class="footer">
    <li class ="footerTitle"> List 3 </li> 
    <li>Sub List 2 </li>
    <li>Sub List 3 </li>
    <li> Sub List 4 </li>
    <li>Sub List 5</li>
</ul>
<ul class="footer">
    <li class ="footerTitle"> List 4 </li> 
    <li>Sub List 2 </li>
    <li>Sub List 3 </li>
    <li> Sub List 4 </li>
    <li>Sub List 5</li>
</ul>
<ul class="footer">
    <li class ="footerTitle"> List 5 </li> 
    <li>Sub List 2 </li>
    <li>Sub List 3 </li>
    <li> Sub List 4 </li>
    <li>Sub List 5</li>
</ul>

Answer №1

I have a solution for what you need. Here is an example that may work:

$(".Navigation > list item").live({
    mouseover:function(){
        $(this).addClass("menuHover");
        $(".lowerNav > list item:first-child").eq($(this).index()).addClass("menuHover");
    },
    mouseout:function(){
        $(this).removeClass("menuHover");
        $(".lowerNav > list item:first-child").eq($(this).index()).removeClass("menuHover");
    },
    click:function(){
        $(".Navigation > list item").removeClass("menuClicked");
        $(this).addClass("menuClicked");
        $(".lowerNav > list item:first-child").removeClass("menuClicked").eq($(this).index()).addClass("menuClicked");
    }
});

$(".lowerNav > list item:first-child").live({
    mouseover:function(){
        $(this).addClass("menuHover");
        $(".Navigation > list item").eq($(".lowerNav").index($(this).parent("ul"))).addClass("menuHover");
    },
    mouseout:function(){
        $(this).removeClass("menuHover");
        $(".Navigation > list item").eq($(".lowerNav").index($(this).parent("ul"))).removeClass("menuHover");
    },
    click:function(){
        $(".Navigation > list item").removeClass("menuClicked");
        $(".lowerNav > list item:first-child").removeClass("menuClicked");
        $(this).addClass("menuClicked");
        $(".Navigation > list item").eq($(".lowerNav").index($(this).parent("ul"))).addClass("menuClicked");
    }
});

Check out the DEMO: http://jsfiddle.net/dirtyd77/WkZuv/65/

Answer №2

A problem arises with the statement provided below.

$(".Nav > li").eq($(this).index()).addClass("menuClicked"); 

The function $(this).index() consistently returns zero in this case. Consider revising your CSS selector to target the group of ul.footer elements in order to obtain the accurate index. However, some additional adjustments will be necessary as well.

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

Ways to prevent users from manually inputting dates in date fields

I am currently developing an application and I need to prevent users from manually entering a date in the type=date field on the HTML page. I want to restrict users to only be able to select a date from the calendar display, which is in the format MM/DD/YY ...

Attempting to retrieve user information from Blockstack on a web browser

Currently developing a web application integrating Blockstack and encountering challenges with Javascript implementation and understanding how to effectively use Blockstack in the browser. My issue arises when retrieving the Blockstack user's ID or u ...

The AppBar is consuming space and obstructing other elements on the

As I dive into React/Material-UI and learn the ropes of CSS, I've come across a challenge with my simple page layout featuring an AppBar. Unfortunately, this AppBar is overlapping the elements that should be positioned below it. In my quest for a sol ...

Unable to maintain the height of a div at 100% as it keeps reverting back to 0px

I'm having trouble setting the height of the individual parent columns (and their children) to 100% because for some reason, the div height keeps resetting to 0. The first column contains two child boxes that should each take up 50% of the page heigh ...

Exporting ExpressJS from a TypeScript wrapper in NodeJS

I've developed a custom ExpressJS wrapper on a private npm repository and I'm looking to export both my library and ExpressJS itself. Here's an example: index.ts export { myExpress } from './my-express'; // my custom express wrap ...

eliminating various arrays within a two-dimensional array

I need help with a web application that is designed to handle large 2D arrays. Sometimes the arrays look like this: var multiArray = [["","","",""],[1,2,3],["hello","dog","cat"],["","","",""]]; I am looking to create a function that will remove any array ...

I'm curious about how to use JQuery to pinpoint the firstName within a JSON String and retrieve its corresponding ID

Does anyone have an idea about the outcome in the alert? Is it a regular string, an object, or JSON? How can I select one of the entities and find another based on that selection? For example, I want to choose the first name and retrieve the ID from it. It ...

Send submitted form field arrays to the database

I am currently developing a sewing management app that includes an order page where users can place multiple orders. However, all orders need to be invoiced with one reference code. On the first page, I collect basic details such as pricing, and on the nex ...

Incorporate a secondary (auxiliary) class into ReactJS

Looking to create a helper class similar to this: export default class A { constructor() { console.log(1); } test() { console.log(2); } } and utilize it within a component like so: import React, { Component } from "react"; import A from ...

Fetching JSON data using Javascript/JQuery

My goal is to access JSON data from an online web service that queries a MySQL database for a specific string and use Javascript to present it on an HTML page. My challenge lies in effectively displaying the retrieved information. The relevant part of my ...

Personalizing Google Map pin

I found some code on Codepen to add pointers to a Google map. Currently, it's using default markers but I want to change them to my own. However, I'm not sure where in the code to make this update. Any examples or guidance would be appreciated. ...

Implementing jQuery time picker on dynamically created elements (e.g. with an 'Add more' button)

I have incorporated the jquery time picker into my project. It works perfectly for existing elements, but when I try to use it on dynamically created text fields within a form generated by JavaScript with an "Add More" button, it doesn't function as e ...

Attempting to transmit unique symbols using JQuery Ajax to a PHP script

I am using JQuery Ajax to send special characters to a PHP file. sending_special_characters.js var special_chars = '!@#$%^&*()_+-='; var dataToSend = 'data=' + special_chars; $.ajax({ type: "POST", ...

What could be causing the issue with my Angular integration with Jira Issue Collector to not function properly?

Looking to link the Jira issue collector with an Angular application I attempted something along the lines of Component.ts import { Component, OnInit } from '@angular/core'; import * as jQuery from 'jquery'; declare global { inter ...

Another class is overriding the border of the text-box

I'm facing a challenge in customizing the material ui css. Specifically, I want to set the border color of a textbox to red. The issue arises when the bottom border gets overwritten. Upon investigation, I discovered that the culprit is the class MuiIn ...

Creating a div caption that mimics a form label, but with the background div border removed, can be achieved

Is there a way to create a div caption similar to a form label (positioned in the middle of the border), but without the div's border interfering? The issue is that I can't just use a background color for the H1 (caption) element because there is ...

Transform the jQuery each method into vanilla JavaScript

I have a script that displays a dropdown in a select box. The current script I am using is as follows: jQuery.each( dslr, function( index, dslrgp) { var aslrp= dslrgp.aslrp; jQuery.each( aslrp, function(index2, pslrp) { var found = 0; ...

I am having trouble retrieving the content-length from the response headers within the app.use() middleware function

Can anyone help with retrieving the content-length from response headers within the app.use() middleware function? const app = express(); app.use((req, res, next) => { // Need to find a way to access content-length of res console.log("co ...

Attempting to designate a comment as private or public by utilizing a checkbox. Employing AJAX to send data to PHP and store it in a MySQL

Currently, the checkbox is returning an [objectO instead of a simple YES or NO value. Currently, my checkbox field is set up as a VARCHAR with a length of 3, but I'm wondering if I should be using TINYINT or Boolean instead. If so, what changes do I n ...

Dynamic image loading in React with custom properties

I'm facing an issue while trying to display an image using the source stored in this.props.src. The correct name of the image file I want to load, "koolImg", is being output by this.props.src. I have imported the file using: import koolImg from ' ...