Tips for maintaining the active state of a link when loading a new page

Currently, I am working on a website project that utilizes Bootstrap framework. One section of the website consists of three buttons, each linking to a different page within the site.

<div class="container-fluid filter-tag py-3">

    <ul class="tag-menu">
        <li><a href="<?php echo esc_attr( add_query_arg( 'tag', 'biella' ) ); ?>" class="btn"> Biella </a></li>
        <li><a href="<?php echo esc_attr( add_query_arg( 'tag', 'vercelli' ) ); ?>" class="btn"> Vercelli </a></li>
        <li><a href="<?php echo esc_attr( add_query_arg( 'tag', 'valsesia' ) ); ?>" class="btn"> Valsesia </a></li>
    </ul>
    

</div>

To indicate the active state, I have applied CSS styling which changes the background-color and text color when a button is clicked. This provides visual feedback for users as they navigate through the website.

.tag-menu li a{
    border:1px solid $primary;
    border-radius:12px;
    padding:5px;
    margin-right:7px;
    color:#000000;
}
.tag-menu li a:active{
    border:1px solid $primary;
    background-color:$primary;
    border-radius:12px;
    padding:5px;
    margin-right:7px;
    color:#ffffff;
    box-shadow:none;
}

Upon testing, I noticed that the active class only appears briefly when a button is clicked before reverting back upon page load. Despite attempting various solutions found on similar issues on Stack Overflow, I have not been successful in resolving this issue with JQuery or JavaScript.

If anyone could offer guidance on how to address this problem effectively, it would be greatly appreciated. Thank you.

Answer №1

:active is not a class, but a selector that detects the clicking moment (between the click and the release), so your CSS is applied only for that instant. Here's a brief example:

a:active{
  color:red;
  }
<a>link example</a>

To achieve the desired effect, you can use an event listener to add a class when the link is clicked:

var links = document.querySelectorAll(".link");
for(link of links){
  link.addEventListener("click", function(e){
    for(inlink of links){
      inlink.classList.remove("active");
    }
    e.target.classList.add("active");
  });
}
.active {
  color:red;
}
<a class="link">link 1</a><br>
<a class="link">link 2</a>

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

"Uploading Images Using AJAX: A Step-by-Step Guide

I'm currently facing an issue with a form in my application. The form consists of 2 text inputs and 1 file upload input. While the text inputs are functioning properly with AJAX, the file upload is not working with AJAX. It works fine without AJAX, bu ...

Ensure to incorporate tab codes within the JSX file for proper rendering

Having trouble integrating my tab code into a jsx file. Whenever I try to include my tab in the jsx, a syntax error occurs: ./src/views/summary/summary-view.jsx Module build failed: SyntaxError: C:/workspace/src/views/summary/sports.jsx: Unexpected token ...

The code appears disorganized; inquiring about alternative methods

Hello everyone! I'm new to front-end web development and this is my first time posting a question here. I've been experimenting with Flask and my code feels a bit messy. Is there a more efficient way to implement the following code? ...

Embed HTML code to a website using a PHP script on a separate webpage

As a newcomer to PHP, I may have a silly question. Let's say I have a form: <form id="createNewGallery" name="newgallery" method="GET" action="code.php"><p><strong>please choose the number of pictures you want to upload: </str ...

Vue.js: V-Model input fails to update with JS virtual keyboard

I have integrated JSkeyboard into my webpage and am using v-model to dynamically update the text based on user input. The issue I am facing is that when using a physical keyboard, everything works as expected. However, when using the on-screen JS keyboard, ...

Mastering the art of selecting checkboxes and Font Awesome icons with JQuery

I find selecting elements using JQuery by their ID or classname quite challenging and it's a bit outside of my expertise. When a function is called, I would like to apply the following styles: .classname input[type="checkbox"] ~ i.fa.fa-circle-o { ...

Prevent the propagation of a particular CSS class's inheritance

I need to make modifications to an existing HTML5 app that features two unique themes. Here's an example of the current structure: <body class="theme1 theme2"> <div id="div1">I'm satisfied with both themes</div> <di ...

Create an HTML button on the homepage that directs users to the "about" page

I've been struggling to make a button in my Ionic app navigate to a different page upon clicking. Despite being new to Ionic, I've spent hours trying to solve this issue. Below is the HTML code in home.page.html: <ion-header> &l ...

Can you provide a succinct explanation of what constitutes a well-defined "landing page"?

Could someone provide a clear and concise explanation of what exactly constitutes a landing page and how it should be utilized? I'm struggling to grasp the concept. Where is the optimal placement for a landing page on a website? Is it best placed o ...

When dynamically binding onclick events within a for loop, all buttons end up with the same value

I'm currently working on a project where I am creating buttons dynamically and assigning an alert function for each one upon a click event. Here is the JavaScript function: function GenerateButtons(tabObj) { for (var t = 0; t < tabObj.views.vie ...

What is the method to display only icons without text when the sidebar is minimized?

check out this image for reference I need the text to be hidden and only display the icons when the sidebar menu is toggled and the sidebar container is collapsed <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1 ...

Having difficulty finding the element in Selenium WebDriver while using C#

Can anyone help me locate the element in the following HTML tag? I keep getting an exception: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='divMain']/div/app-train-list/div ...

Ensuring Radio Button Selection is Valid

Has anyone had success validating radio buttons using Javascript? I'm having some trouble and would appreciate any help. Here is the code I've been working with: <form action="submitAd.php" method="POST" enctype="multipart/form-data" name="pa ...

Run a directory using the Node.js command line interface

Recently, I explored a node.js web framework known as feathers.js. Following the provided example: $ npm install -g @feathersjs/cli $ mkdir my-new-app $ cd my-new-app/ $ feathers generate app $ npm start Within the package.json file in the example, there ...

Indicate the end of the row in JavaScript when using the match() function

What's the best way to identify the end of a row when using the match() function? I'm trying to extract "2021 JANUARY 18 MONDAY" from this page. https://i.sstatic.net/bTNdQ.png https://i.sstatic.net/YzFs2.png If there is additional text after ...

Adjust the tabs to fit perfectly within the container

I am currently facing an issue with my navigation bar. I have placed the <nav> tags within a container set to 100% width. However, when I adjust the height of the container by a certain percentage, the <ul> & <li> elements in the nav ...

Refreshing Datatable without having to refresh the entire webpage

I apologize for my limited English proficiency. Could you please explain to me the process of reloading data without refreshing the page using jQuery's datatable? I have been encountering issues with my table not properly reloading. Here is my view: ...

Display only the background image or color behind a stationary element

Our website features a header with a fixed position that remains at the top of the page while scrolling. The product team has requested a gap or margin between this fixed element and the top navbar. However, when we implemented this gap, users started not ...

Angular ngRoute causing issues when loading a chart

I have been attempting to integrate a chart using the Highcharts library in an Angular application with ngRoute, but I am facing difficulties in displaying the chart. This is my JavaScript configuration: var app = angular.module('myApp', [ ...

VueJS: A Closer Look at Parent-Child Communication

I have created two Vue components. The first one is called parent-component: Vue.component('parent-component',{ methods: { test: function(){ alert('Option Selected'); } }, te ...