jQuery Update for Header/Footer Navigation

I have implemented header and footer navigation with different states using jQuery. However, I am encountering an issue when trying to update the header nav upon clicking on the footer nav items. Specifically, I want the header nav to reflect the same selection as the footer nav when a user clicks on a list item. Please refer to the jsfiddle example I provided.

Update: The header nav contains the main list items, while the footer nav includes both main list items and sub-navs. My goal is to achieve the same functionality where clicking on the first list item labeled "footerTitle" updates the header nav accordingly. I have made changes to the jsfiddle link.

http://jsfiddle.net/WkZuv/40/

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

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

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


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

<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

Take a look at this awesome code snippet http://jsfiddle.net/WkZuv/28/

It's important to consider caching your jQuery objects for better performance.

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

$(".footer > li").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");  
    } });

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

`The Dropdown Component in TailwindCSS/daisyUI Does Not Adhere to Color Customization`

Currently, I am utilizing TailwindCSS and daisyUI to construct a dropdown menu. The code for it can be found below. The issue that I am facing is when a dropdown menu item is selected (activated), it turns purple, despite applying the utility class bg-whi ...

Angular material dialog box experiencing issues with saving multiple values

Currently, I am utilizing Anular9 and implementing mat-raised-button in the following manner: <button mat-raised-button (click)="saveClick()" color="primary"><mat-icon>check</mat-icon>&nbsp;&nbsp;Ok</butto ...

Send a JSON data to an MVC controller and fetch an IEnumerable data structure

I am trying to fetch a JSON object through AJAX, structured as: {ID_USER : ID_INSTRUCTION} The fetched JSON looks like this: [{"658":"81"},{"658":"82"},{"658":"90"},{"658":"101"}] Below is my jQuery code for handling the AJAX request: $("#boutton-add" ...

Repeated actions using jQuery

Being new to jQuery, I am currently exploring ways to repeat functions without the need to continuously write them over and over again. In the code snippet below, you can see that when a button is clicked, it changes to 'I was clicked' in an h1 t ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

Customizing the color of cells in an HTML table within a JSON based on their status

Would you like to learn how to customize the color of cells in an HTML table based on the status of a college semester's course map? The table represents all the necessary courses for your major, with green indicating completed courses, yellow for cou ...

Encountered an Angular HPM localhost error while attempting to proxy a request for /api/books from localhost:4200 to http://localhost:3333

After spending several hours attempting to resolve this issue, I am still unable to successfully make a post request due to an error. Initially, I used ng serve, but switched to npm start and the errors persist. Error: [HPM] Error occurred while attempti ...

Comparing textboxes on separate web pages to validate forms using AJAX on the server side

I am exploring the creation of a straightforward server-side form validation system using ajax. The objective is to verify if the data inputted by the user matches the data stored on another page. For instance, if a user enters the number 10 in a textbox ...

Utilizing Express Routes to specify React page components

Currently, as I delve into the world of learning Express.js, I find myself faced with a unique scenario involving 2 specific URLs: /security/1 /security/2 As per the requirements of these URLs, the expected response will vary. If the URL is "/securi ...

Rails 3, leveraging the power of JQuery in combination with RJS (UJS)

Attempting to enable ajax callbacks on a href using remote. Have Rails.js and JQuery1.6 installed. Trying to catch the callback with this code: $('#choo').bind('ajax:success', function(){ alert("Success!"); }); Here is my div an ...

How to adjust the "skipNatural" boolean in AngularJS Smart-Table without altering the smart-table.js script

Looking to customize the "skipNatural" boolean in the smart-table.js file, but concerned about it being overwritten when using Bower for updates. The current setting in the Smart-Table file is as follows: ng.module('smart-table') .constant(&ap ...

What could be causing my hover effect to function exclusively on Chromium-based browsers and not on Firefox?

Could use some help with this code snippet .podmenu { display: flex; flex-direction: column; list-style: none; width: 10rem; margin-left: 3rem; margin-bottom: 60px; } .verze { list-style: none; flex-direction: column; ...

Trigger the React useEffect only when the inputed string matches the previous one

Currently, I am in the process of creating my own custom useFetch hook. export const useFetch = <T extends unknown>( url: string, options?: RequestInit ) => { const [loading, setLoading] = useState(false); const [error, setError] = ...

What is the best way to assign an object variable within an array state?

My current state looks like this: const [tags, setTags] = useState([{id: 1, label: "random"}, {id: 2, label: "important"}]) const [input, setInput] = useState("") const [selectedTag, setSelectedTag] = useState([]) In addition ...

Is there a way to retrieve the day of the week based on the date provided by the user

function retrieveWeekday() { var input = document.getElementById("input"); } <form> <input type="date" placeholder="dd:mm:yy" id="input"/> <input type="button" value="get weekday" onclick="retrieveWeekday()"/> ...

Scrollable content below the div

I have encountered an issue where a div is positioned above another div, and when I scroll the upper div to the bottom, it ends up scrolling the entire body. To better illustrate this problem, I have created a demo on JSFiddle: http://jsfiddle.net/2Ydr4/ ...

"An error occurred: Uncaught SyntaxError - The import statement can only be used within a module. Including a TypeScript file into a

I need to integrate an Angular 10 TypeScript service into a jQuery file, but I am facing an issue. When I try to import the TypeScript service file into my jQuery file, I encounter the following error: Uncaught SyntaxError: Cannot use import statement outs ...

Transfer the data from the MySQL database directly into the textarea, ensuring there are no additional spaces included

Having an issue with my code: The formatting is preserved when saving to the database, but extra spaces appear in the textarea input. Screenshot: <?php $id = $_GET['id']; ?> <form id="form1" name="form1" method="post" enctype="mu ...

The challenge with using Telerik Rad Textboxes in conjunction with JavaScript

I am facing an issue where the server data is not displayed in a Rad Textbox until I click on it using JavaScript. The data is being populated correctly inside the textbox, but it remains invisible until clicked. function fillTextBox(name, sku, plu, nam ...

What are some methods for creating a responsive table design?

Here is my table that needs to be made responsive: <table style="width:100%" class="table table-hover"> <tr> <th>Booking Date</th> <th>Booking Id</th> ...