Use jQuery to select and emphasize the following menu option

Hey there! I'm currently working on adding a new feature to my website. I want the menu items to highlight in succession when clicked. For example, if I click on people, I want it to highlight and then move on to highlight the next item in the menu, which would be tourist. I've been using CSS for hover effects, but I've heard that a:active doesn't work with CSS. Can anyone provide some guidance on this?

Here's what I have done so far:

HTML

<section id="nav">
        <li><a class="nav" href="People.html">People</a></li>
        <li><a class="nav" href="Tourist.html">Tourist</a></li>
        <li><a class="nav" href="Joints.html">Joints</a></li>
        <li><a class="nav" href="Project.html">Project</a></li>
        <li><a class="nav" href="Products.html">Products</a></li>
        <li><a class="nav" href="cafes.html">cafes</a></li>
    </section>

jQuery

<script>
        $(function() {
            $('#nav').on('click','.nav', function ( e ) {
                e.preventDefault();
                $(this).parents('#nav').find('.active').removeClass('active').end().end().addClass('active');
                $(activeTab).show();
            });
        });
    </script>

CSS

#nav{
        width:100%;
        text-align:center;
        min-width:1300px;
        height:80px;
        position:absolute;
        top:0;
        left:0;
        background:#fff;
        list-style:none;
        border-bottom: 1px solid #000;
    }

    #nav li{
        display:inline;
    }

    #nav .nav{
        display:inline-block;
        background-color:#000;
        color:#FFF;
        font-family: 'Oswald', sans-serif;
        letter-spacing:1px;
        font-size:16pt;
        line-height:18pt;
        font-weight:400;
        text-decoration:none;
        margin-right: 3px;
        margin-left: 3px;
        margin-top:35px;
        padding:0px 3px 2px 3px;
    }

    #nav .nav:hover{
        background:#FFFF00;
        color:#000;
    }

    .active{
        background:#FFFF00;
        color:#000;
    }

If you have any tips or suggestions, please share! I'm currently stuck on this issue and any help would be greatly appreciated.

Answer №1

Here is a solution that may work for you (assuming I have correctly understood your question):

$('#nav').on('click','.nav', function ( e ) {
    e.preventDefault();
    // Remove all instances of the "active" class:
    $('.active').removeClass('active');
    // Locate the next menu item and add the "active" class:
    $(this).parent().next('li').find('.nav').addClass('active');
    $(activeTab).show();
});

To ensure that the styles for .active are applied (especially if they are being overridden by parent styles like #nav .nav), add !important to the CSS:

.active{
    background:#FFFF00 !important;
    color:#000 !important;
}

Check out the JSFiddle demo

Answer №2

To emphasize the following item in the menu, you just need to identify the index of the menu item (the index of the <li> in this case) and then determine the next one:

UPDATE:

included animation in code snippet

$(function () {
    $('#nav').on('click', '.nav', function (e) {
        e.preventDefault();
       var NextMenuID = ($(this).parent().index()+1)%$(this).parent().parent().children().length;
        var NextItem =$('#nav .nav').eq(NextMenuID);
        $('#nav .nav').removeClass("active");
        var x1=$(this).offset().left;
        var y1=$(this).offset().top;
        var width1=$(this).width();
        var height1=$(this).height();
        
        var x2=NextItem.offset().left;
        var y2=NextItem.offset().top;
        var width2=NextItem.width();
        var height2=NextItem.height();
        var slidingDiv=$("<div/>");
        slidingDiv.css({
            "width":width1,
            "height":height1,
            "left":x1,
            "top":y1,
            "display":"block",
            "position":"absolute",
            "background":"#FFFF00",
            "padding":"0px 3px 2px 3px"
        }).appendTo($("body")).animate({
            "width":width2,
            "height":height2,
            "left":x2,
            "top":y2,
        },function(){ 
            NextItem.addClass("active");
            slidingDiv.remove();
        });
        
      
        //$(activeTab).show();
    });
});
#nav {
    width:100%;
    text-align:center;
    height:80px;
    position:absolute;
    top:0;
    left:0;
    background:#fff;
    list-style:none;
    border-bottom: 1px solid #000;
}
#nav li {
    display:inline;
}
#nav .nav {
    display:inline-block;
    background-color:#000;
    color:#FFF;
    font-family:'Oswald', sans-serif;
    letter-spacing:1px;
    font-size:16pt;
    line-height:18pt;
    font-weight:400;
    text-decoration:none;
    margin-right: 3px;
    margin-left: 3px;
    margin-top:35px;
    padding:0px 3px 2px 3px;
}
#nav .nav:hover {
    background:#FFFF00;
    color:#000;
}
.active {
    background:#FFFF00 !important;
    color:#000 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="nav">
    <li><a class="nav" href="People.html">People</a>

    </li>
    <li><a class="nav active" href="Tourist.html">Tourist</a>

    </li>
    <li><a class="nav" href="Joints.html">Joints</a>

    </li>
    <li><a class="nav" href="Project.html">Project</a>

    </li>
    <li><a class="nav" href="Products.html">Products</a>

    </li>
    <li><a class="nav" href="cafes.html">cafes</a>

    </li>
</section>

If you have any inquiries regarding the provided example, don't hesitate to ask.

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

Bug in canvas rendering for Chrome versions 94 and 95

The Canvas drawing functionality in the Chrome browser seems to have some visible bugs. Here's an example of code that demonstrates this issue: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d&apo ...

Is it advisable to build dynamic pages using jQuery Mobile?

I'm currently working on a phonegap/jQuery-Mobile app that dynamically populates a list from the Google Books API based on user input such as an author's name or book title. When a user clicks on a list item, I want a new page to display with the ...

Avoid making an Ajax request in CodeIgniter if there is no active session or if the network is disconnected

I am currently facing two challenges in my project. I would greatly appreciate it if someone could assist me with resolving them. One issue I am encountering is with the use of the .load() function in JavaScript to load another page using Ajax. While i ...

Why isn't it possible to send POST data to a JSON file using JQuery/AJAX?

I'm currently learning how to use JQuery/Ajax from a helpful tutorial on YouTube. To watch the video, simply click here. While I can successfully retrieve data from the order.json file, I encounter an error whenever trying to send POST requests. Bel ...

Dynamic Rendering and Retrieving Component HTML in Angular

Generate the HTML code of a component to open a new tab with an about:blank page. Reason: This method helps avoid creating HTML using string variables, such as: var html = '<div> <h3>My Template</h3> &a ...

Arrange the menu items in a column layout based on a given condition

Can someone assist me with displaying the menu subitems? I have created a plunker. Take a look at it to understand what I need (open plunker in full screen) https://plnkr.co/edit/IMEJFPfl5kavKvnUYaRy?p=preview In the plunker above, there are two dropdown ...

Jupyter QtConsole: Customize default CSS by selecting from built-in options in the configuration settings

I am currently using Jupyter on a Windows platform and I am looking to customize the coloring of the QT console. Is there a way to set a default CSS style through a configuration file without having to manually specify it every time? Instead of passing t ...

Using a set formatter in jqGrid within a personalized formatter

Can I incorporate a predefined formatter into a custom formatter? Here is an example using the colModel: colModel: [ ... { name: 'col1', formatter: myFormatter } ... ] Below is the custom formatter function: function myFormatter(cellVal ...

Are Global variables supported in the EXT JS framework?

When working with Java and C++, it is common practice to store a variable globally so that its value can be accessed from anywhere within the project. For example, if I am working inside a class named Residence and saving an INT as the residenceNumber to a ...

Can other JavaScript event listeners be synchronized with an asynchronous event?

My goal is to manipulate a web page using Tampermonkey where there is a button with certain event listeners followed by a redirect. Is it possible for me to insert my own click event handler into the button, which triggers an AJAX request and waits for it ...

Transitioning images with jQuery

For my college project website that focuses on selling music, I have implemented a buy button that resembles the one seen on Apple's app store. You can view the images of the button here: View Image 1 View Image 2 I am wondering if anyone is famili ...

Tips for achieving proper styling and formatting of elements in jQuery UI

I've encountered an issue when trying to use jQuery UI after downloading it multiple times. In the examples provided with the download, the UI elements appear perfectly formatted, but when I implement them on my own pages, the styles and formatting ge ...

Utilizing the power of AWS Lambda in conjunction with moment JS to generate unique

My current time zone is GMT+8, and the AWS region I am using is Singapore (ap-southeast-1). I am facing an issue where there are discrepancies in date calculations between my local machine and when I deploy my code on AWS Lambda. My goal is to ensure that ...

Is optgroup malfunctioning in IE 10?

My main question is: does optgroup not function properly in IE? The Plnkr code works fine in Chrome but encounters issues in IE 10. I'm trying to find a solution that will work across both browsers. Is this a known problem? In Chrome, I can expand/co ...

Troubleshooting a problem with placeholder styling on Internet Explorer

Encountering a challenge while styling placeholders, particularly in Internet Explorer where the placeholder text color does not change as expected. Instead, it inherits the color of the input field. :-ms-input-placeholder { color: white; } input ...

Running a JavaScript function within a designated div element

I'm currently facing an issue with executing a javascript function - onload only in a specific div. I seem to be stuck on this problem, so if anyone could offer some assistance, I would greatly appreciate it. Thank you very much in advance! functio ...

Removing jQuery error label from an HTML block

I need a single command that will remove an error label from my HTML when the field content is changed. It currently works on input and select elements, but not within an input-group. I am looking for a solution that will work universally across all instan ...

Using Angular 2: A Beginner's Guide to Navigating with the Latest Angular 2.0.0-rc.1 Router

As I embarked on a new Angular 2 project, I was puzzled to discover that I inadvertently installed two different versions of the angular router: "@angular/router": "2.0.0-rc.1", "@angular/router-deprecated": "2.0.0-rc.1", Despite my best efforts, I co ...

Issue with Ajax and jQuery

As a novice in the world of Ajax and jQuery, I could really use some guidance... I have an XML file that I want to integrate into my webpage using the following URL: However, when checking with Firebug, it appears that nothing is returning. What mistake ...

Main content with a floating aside menu

I am currently using the CoreUI admin template. The layout I have set up is the basic one outlined on the following page: My goal is to make the aside menu appear on the right side of the main content, floating over it rather than inline and reducing the ...