The background color of the active tab is updated upon loading the page

I have attempted to modify this code to change the background color of li tag on click. It successfully changes the background color when hovering or clicking, but unfortunately reverts back to the default color upon page refresh. I am looking for a solution to maintain the active tab's background color even after reloading the page.

Please provide guidance.

  <script src="http://code.jquery.com/jquery-1.7.2.js"></script>
  <script type="text/javascript">
      $( document ).ready( function(){
          $('.nan_canada').on('click','.menuil',function () {
             $(this).addClass('selected')
          });
      });
  </script>

 <div class="nan_canada">
     <ul >
        <li class="menuil" ><a href="a.php">Resume Template & LinkedIn Podcasts</a></li>
        <li class="menuil"><a href="jas.php" >Job Postings </a></li>
        <li class="menuil"><a href="a.php">   Search Firms</a></li>
        <li class="menuil"><a href="aj.php" target="_blank"> nav</a></li>
        <li class="menuil"><a href="books.php">Book List </a></li>
        <li class="menuil"><a href="#"> Order Free Book </a></li>            
        <strong><font size="4" style="float:right; padding-right:10px;"><a href="at.php" style="color:#fff;">Log Out</a></font></strong> </ul>
    </div>

    <style type="text/css">
        .menuil {
               color:#000000;
         }
        .menuil  #selected_step_box, 
        .QuickStartLong:hover {
               background-color: #fff; !important
         }
         .selected {
               background-color : #fff;
         }
     </style>

Answer №1

One of the reasons for this behavior could be redirection - when you click a link, it redirects you to a new page and reloads the script every time. One way to address this is by assigning a unique id to each list item, like so:

<li class="menuil" id='l1'><a href="a.php">Resume Template & LinkedIn Podcasts</a></li>
                                                    .
                                                    .
                                                    . 

Additionally, you can create a JavaScript file and include it in all list pages with the following code:

var page = $(location).attr('href');

switch(page)
{
  case 'youpage1':
    $('#l1').addClass('selected');
    break;
   case 'youpage2':
    $('#l2').addClass('selected');
    break;
   .
   .
   .
}

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 is the process for modifying the attributes of a currency text box in a Dojo within XPages?

I have come across several discussions aimed at addressing this issue, but none of them have provided a clear and definitive solution that I can easily follow. Is there a straightforward method to modify the css of a dojo field within XPages? Specifically ...

Convert JSON data from jQuery into a modal form within a div element

I am currently working on an MVC application and attempting to populate a bootstrap modal form with data from the server. My approach involves: 1) Loading a grid with various IDs, each linked to a javascript onclick function. 2) The function retrieves th ...

How can I implement the self-clearing feature of a timer using clearInterval in ReactJS?

I am a newcomer to the world of React and I am currently working on creating a React component with multiple features. Users can input a random number, which will then be displayed on the page. The component includes a button labeled 'start'. W ...

Applying personalized CSS to an element based on the condition of a child element in the previous sibling element

I am trying to apply styles to a span element only when a child element in a preceding sibling div element is active, which means a radio button has been checked. I am unable to modify the code and can only use CSS for this task. Any ideas on how to achi ...

Surprising outcome encountered while trying to insert an item into a list

I'm a bit puzzled by the current situation where: let groupdetails = { groupName: "", }; const groupsArray = []; groupdetails.groupName = 'A' groupsArray.push(groupdetails) groupdetails.groupName = 'B' groupsAr ...

Encountering both a CORS error and data in Angular 7

My application is using Angular 7 for the front end and Node.js (express) for the backend. The cors module in the Node.js server.js file is implemented like this: var cors = require('cors') app.use(cors()); When making an API call from the fro ...

Is there a way to extract information from an HttpClient Rest Api through interpolation?

I am currently facing an issue with a component in my project. The component is responsible for fetching data from a REST API using the HttpClient, and the data retrieval seems to be working fine as I can see the data being logged in the Console. However, ...

Unable to display <div> elements from CSS within the Wordpress Text Widget

Having trouble displaying a <div> from CSS in a Text Widget on Wordpress. <div class=”deffgall-cutlure” ></div> style.css .deffgall-cutlure { display:block; position:relative; width:100px; height:100px; backgrou ...

v-treeview component triggering method execution twice upon input

I'm facing an issue with my Vue component that contains a treeview. Upon selecting a node, the goal is to update an array and display the checkbox as selected. However, I'm encountering a problem where if I select elements from one folder in the ...

How to make background image fade in with jQuery on hover

I want to create a row of menu buttons with transparent background-images that fade in on hover. Is it possible to achieve this effect using jQuery? Check out an example here! ...

Is it necessary to use the "new" keyword when utilizing JS closure to create objects?

My response to a question about closures on SO included the following code sample: function Constructor() { var privateProperty = 'private'; var privateMethod = function(){ alert('called from public method'); }; ...

Placing a material UI Radio button within a row cell on a table

Here is the current design snapshot: I am working with Material UI's radio button component and I want to ensure that each row in the table can be selected only once. Although I am able to select a single row using the <RadioButton> component, ...

What are the advantages of utilizing NGRX over constructor-injected services?

Have you ever wondered about the benefits of using NGRX or NGXS for an Angular application instead of constructor injected services to manage component IO? Is it simply to prevent mutation of component properties references without replacing the entire pr ...

Unexpected Visual Basic Web Label Formatting

I am facing an issue with the styling of a complex label. The CSS code doesn't always apply as expected. Interestingly, when I ran this code on two different machines, the styles were inconsistent. The code seems messy and disorganized to me, but al ...

I noticed an excess of white space on the right side of my Angular website

Check out my website here. Everything seems to be functioning correctly, however, there is an issue with scrolling on mobile. It should only scroll up and down, not left and right! I have noticed a strange white space on the right side of my site when view ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Having trouble pushing to an array in Angular typescript? It seems to be returning as undefined

As a newcomer to Angular with a basic understanding of JavaScript, I am attempting to create a program that can solve Sudoku puzzles. In a 9x9 grid, there are a total of 81 points or squares. To efficiently check for violations of Sudoku rules - no repeati ...

Using AngularJs to have two ui-views on a single page

As a beginner in AngularJs, I encountered an issue with having two ui-views on the same page. When I click a button to show the view for goals, both the form for goals appear in ui-view 1 and ui-view 2 simultaneously. I have been working with states but I ...

ng-model assigns the value to the select

Below is the angularjs directive I have created: dicApp.directive('listpanel', function ($resource) { return { restrict: 'E', template: '<select ng-model="selectedDictionary" ng-change="listChange()">&apo ...

Invoking a React function repeatedly (every second)

Currently, I am working with React and utilizing Material UI to create a modal. The modal is rendered as part of the body of the code, placed at the bottom of the page. Its visibility is controlled by the state; if it's open or closed. However, I&apos ...