Utilizing Javascript to showcase the active tab on a navigation bar

I have set up a bootstrap side navbar on my website. I am looking to indicate to the user which tab has been selected by maintaining the active css property. Here is what the side navbar code looks like:

    <div style="font-size: 80%; background-color: #ffffff; width: 15%;" class="col-xs-6 col-sm-3 sidebar-offcanvas">
    <ul class="nav">
        <li >
            <asp:LinkButton ID="LB_MainPage" CssClass="activeBtn active" runat="server" OnClick="LB_MainPage_Click">Ticket Books Home</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_IssueTicket" CssClass="activeBtn" runat="server" OnClick="LB_IssueTicket_Click">Issue Ticket Book</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_Change_TicketBooks" CssClass="activeBtn" runat="server" OnClick="LB_Change_TicketBooks_Click" >Change Ticket Books</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_TicketBook_Reports" CssClass="activeBtn" runat="server" OnClick="LB_TicketBook_Reports_Click">Search Ticket Books</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_MissingTickets" CssClass="activeBtn" runat="server" OnClick="LB_MissingTickets_Click">Custom Reports</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_AddVoidTicket" CssClass="activeBtn" runat="server" OnClick="LB_AddVoidTicket_Click" >Add Void Ticket</asp:LinkButton>
        </li>
        <li >
            <asp:LinkButton ID="LB_VoidTickets" CssClass="activeBtn" runat="server" OnClick="LB_VoidTickets_Click" >Void Tickets</asp:LinkButton>
        </li>
    </ul>
</div>

I aim to implement the CSS styling as follows:

        li .activeBtn {
        cursor: pointer;
    }
    .activeBtn.active {
        color: purple;
        border-bottom: solid;
    }

Upon clicking a LinkButton, I want to mark that button as active and reset all other buttons to inactive. For this, I have included the following jQuery function in my MasterPage:

$(function () {
          $(".activeBtn").click(function () {
          // remove classes from all 
          $(".activeBtn").removeClass("active");
           // add class to the one we clicked 
          $(this).addClass("active");
          console.log("this is a test log");
          });
          });

The code works correctly in this jsFiddle. However, it seems to not work properly due to the presence of a multi-view on my page. When a LinkButton is clicked, I switch views within the multi-view.

Any insights on why this behavior differs from the jsFiddle example provided?

Answer №1

Check out this https://jsfiddle.net/hwcgynr0/4/
Simply add a class to the clicked element, then select its siblings and remove their active classes

$(function () {
    $('body').on('click', '.activeBtn', function () {
        $(this).addClass('active').closest('li').siblings().children().removeClass('active');
    });
});

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

What's the best way to ensure the BODY element occupies the full height of the HTML element?

Currently, I have implemented the sticky footer example code from Twitter Bootstrap. The sticky footer is functioning correctly, but my goal now is to extend the body (or a div) to occupy the remaining space by matching the height of the html element and a ...

An issue arose while retrieving the AJAX response in Javascript

I'm facing an issue with my project where I am using ajax to send messages. The problem is that I cannot get the response in the ajax function, even though it was working perfectly fine before. I can't pinpoint the exact cause of the issue, so pl ...

It is not possible to utilize a JavaScript function once the script has been loaded through

I am attempting to programmatically load a local JavaScript file - PapaParse library, and then utilize one of its functions: $.getScript("./Content/Scripts/papaparse.js", function () { console.log("Papaparse loaded successfully"); Papa.parse(file, ...

Can the Three.js Orbit Controls be customized to modify their appearance?

While I appreciate the orbit controls that come with Three.js, I am wondering if there is a way to customize them to follow the path of an ellipse (or technically an ellipsoid) instead of a circle. My main goal is to move the camera in an ellipse using th ...

Accessing information from Zabbix API using a Node.js Express application

I'm currently working on pulling data using the Zabbix API in a node application with Express and zabbix node. One issue I've encountered is that when I try to print a variable using console.log, it shows as undefined. I can't figure out why ...

Creating a custom map using React and Leaflet

Can anyone guide me on creating a map using leaflet? I am encountering the following issue. ./src/Map.js Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap'). Below is the code I have ...

What is the best way to implement a conditional data-attribute tag in order to trigger JavaScript functionality?

Is there any way to conditionally apply JavaScript or jQuery code in an HTML Template? For example, if a data-id attribute with the value "no-copy" is found inside the body tag, certain JavaScript code will be applied. To explain it more simply, I have J ...

reduce input to 2 characters using jQuery

Is there a way to trim the input text down to just the first two characters when a button is clicked? For example, if I enter "BT2J43" into the input field, can it be automatically shortened to "BT" upon clicking the button? I'm new to learning jQue ...

Difficulty encountered while trying to access JSON object

I've been utilizing the LinkedIn JS API to retrieve a list of individuals. The data is returned in JSON format, and here is the callback function: .result(function (result) { profile = result.values[0]; // Perform an action with the f ...

Tips for designing a hexagonal chessboard using CSS

I am looking for help with a web development project where I want to create a CSS and HTML version of the hexagonal chess board shown here. My objective is to reproduce the grid structure of the chessboard while excluding the text that accompanies it. Cur ...

What could be causing the issue with my Date and Time input not functioning correctly?

I developed a React frontend application styled with Material UI design. The issue I am facing is that after adding the Date and Time component styling to the form, it is unable to process the "name" value for posting. Specifically, the error message "Ty ...

Cause a malfunction in the triggering function of jQuery

$(document).ready(function(){ jQuery("#but1").bind("click",function(e){ alert(e.name); }); jQuery("#but1").trigger({name:'Dmy', surname:'My'}); }); The information doesn't seem to be getting passed correctly a ...

Running a function exported from a string in Node.js

Is it possible to execute a function with a name stored as a string in Node.js? The code is meant to run on the server side without any browser involvement. If I have a file named test.js exported with the following function: module.exports.test = functi ...

The width of the HTML page seems to adjust or shift when I click on

I'm encountering an issue with my navbar. Whenever I open the dropdown menu, the width of my page changes but returns to normal when it is closed. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="styl ...

What's the best way to include CSS and Javascript in an HTML document?

I'm still getting the hang of CSS and JavaScript. I stumbled upon a cool countdown timer that I'd like to incorporate into my website, but I'm not quite sure how to place it where I envision it. Check out the timer here CSS: html,body{mar ...

Creating a Vue.js component during the rendering process of a Laravel Blade partial view

In my Vue.js project, I have a component that is used in a partial view called question.blade.php: {{--HTML code--}} <my-component type='question'> <div class="question">[Very long text content...]</div> </my-component& ...

Sending parameters in GraphQL with Typescript results in an empty set of curly braces being returned

I am new to learning GraphQL with Typescript and I am trying to pass an argument in a GraphQL function to return something dynamically. I have been struggling with this issue for the past hour and could not find any solutions. Here are the relevant code sn ...

Do we still need to configure XSRF-TOKEN on the server even when using HttpClientXsrfModule?

Would implementing the code below in app.module be sufficient to protect against XSRF/CSRF on the client side? HttpClientXsrfModule.withOptions({ cookieName: 'XSRF-TOKEN', headerName: 'X-XSRF-TOKEN' }) Alternatively, is additional ...

The directory does not contain ChromeDriver and IEDriver

I created a basic test in visual studio using selenium that runs successfully in Firefox. However, when attempting to execute the same test in different browsers, I keep encountering an error stating that the drivers cannot be located in the directory or t ...

Steps for automatically playing the next song when a button is clicked

I have encountered a challenge in developing a music player. The issue lies in the loading of the next song when the user clicks the 'next' button. While the new data is successfully updated in both the state and render, the music does not automa ...