Preserve the font awesome icon when the page is refreshed

My code successfully toggles the visibility of forums on my webpage.

Although the status show/hide is retained after reloading the page,

I have an issue with the font awesome icon reverting back to default.

How can I modify the code to ensure that the font awesome icon remains unchanged during reload?

$(document).ready(function() {
  if (window.localStorage.getItem("#wpf-cat-6 .forum-wrap") != null) {
    var pb = window.localStorage.getItem("#wpf-cat-6 .forum-wrap");
    if (pb == "true") {
      $("#wpf-cat-6 .forum-wrap").hide(); 
    }
  }

  $("#wpf-cat-6 .wpforo-toggle").click(function() {     
    var v = $("#wpf-cat-6 .forum-wrap").is(":visible")
    $("#wpf-cat-6 .forum-wrap").slideToggle("slow");
    $("#wpf-cat-6 .wpforo-toggle i").toggleClass('fa-chevron-up fa-chevron-down'); 
    window.localStorage.setItem("#wpf-cat-6 .forum-wrap", v)
  });   
});

Your assistance is greatly appreciated.

Live Demonstration available at: Fiddle

Answer №1

After experimenting with various code options, I stumbled upon a solution that seems to be working. I'm not entirely sure if everything is accurate, but it gets the job done ;-)

$(document).ready(function() {
  if (window.localStorage.getItem("#wpf-cat-6 .forum-wrap") != null) {
    var pb = window.localStorage.getItem("#wpf-cat-6 .forum-wrap");
    if (pb == "true") {
      $("#wpf-cat-6 .forum-wrap").hide();  
    }
  }

  $("#wpf-cat-6 .wpforo-toggle").click(function() {
    var v = $("#wpf-cat-6 .forum-wrap").is(":visible")
    $("#wpf-cat-6 .forum-wrap").slideToggle("slow");
     $("#wpf-cat-6 .wpforo-toggle i").toggleClass('fa-chevron-up fa-chevron-down');  
    window.localStorage.setItem("#wpf-cat-6 .forum-wrap", v)
  });
    $(document).ready(function () {
    if (localStorage.getItem('#wpf-cat-6 .forum-wrap') === 'true') {
    $("#wpf-cat-6 .wpforo-toggle i").toggleClass('fa-chevron-down fa-chevron-up'); 
    }
});
});

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

Accessing an item within a JSON object using jQuery

Trying to access an element within a JSON object, part of the code is shown below: { " academy": { "business": { "E-commerce": [ I have successfully accessed the academy as the first element using the following code: $.getJSON("p ...

The Strapi registration feature in version 4 is giving a "method not allowed 405" error, which

Feeling a bit confused with a Strapi query. I am currently working on v4 and trying to set up a registration feature. The code snippet for invoking the function is provided below. Everything seems fine, such as submitting function variables, etc. However, ...

Parsing the CSV file contents according to the specified columns

Currently, I'm involved in a project using AngularJS where I need to extract data from a CSV file column by column using JavaScript. So far, I've successfully retrieved the CSV data and displayed it in the console. While I've managed to sepa ...

What are the steps to start using the Intersection Observer API right away?

Utilizing the Intersection Observer API, I can accurately determine whether an element is within the viewport or not. Is there a way to utilize the Intersection Observer API to detect if an element is in the viewport without relying on a callback function ...

Using jQuery to gradually fade out my sidebar (div)

I have written the following code to hide my sidebar when the screen width is less than 1024 pixels, but it doesn't seem to be working. It worked fine on a previous website, so I'm not sure what the issue is here. Any help would be greatly apprec ...

Guide to altering the color of the row that is clicked in a table using css and jquery

Is there a way to change the text color of a row in a table when it is clicked, similar to an example like this but without changing the background color? The code I have tried is not working for me. Below is a snippet of the code I am using: $("#myTab ...

What is the best way to allocate values within a for loop?

I am in the process of designing an interface for individuals who have no background in programming. My goal is to allow them to input certain details, and then be able to simply copy and paste the code to make everything function seamlessly. Here is a sa ...

JQuery 1.9 Javascript file contains a broken link with the path "/a"

During a security audit of our project, we discovered a vulnerability related to a broken link "/a". After thoroughly searching our project, we pinpointed the issue to a link in the JQuery-1.9.js JavaScript file that is being utilized. Within the JQuery- ...

Creating intricate filters using React's map functionality

QUESTION HAS BEEN UPDATED. I am currently working on implementing a filter for my map, allowing users to specify which options they want to view using multiple selection boxes. However, I am encountering an issue with the page that appears after the user ...

Conceal a particular object from view by selecting it from the menu

I want to hide a specific element when the burger button is clicked. (See CaptureElementNoDisplay.PNG for the element to hide) When I click the burger button again to close the menu, I want the hidden item to be displayed once more. I've successfull ...

JavaScript and CSS animations offer dynamic and engaging ways to bring

I'm working on a div section and I want it to come down and rotate when a button is clicked. However, after moving to the bottom, it doesn't rotate as expected but instead returns to its initial position along the Y axis. How can I fix this issue ...

The second JSP page fails to load following an AJAX POST request

The following code snippet is found in page1.jsp. $.ajax({ type: "post", url: "page2.jsp", data: newdata, success:function(msg){ return msg; } ...

React is unable to identify the `activeKey` property on a DOM element

First and foremost, I am aware that there have been a few inquiries regarding this particular error, although stemming from differing sources. Below is the snippet of my code: <BrowserRouter> <React.Fragment> <Navbar className=& ...

Access a component from the @ViewChild without resorting to nativeElement

Is there a way to access the underlying div element using a @ViewChild? I have attempted the following approaches without success: ElementRef.prototype.nativeElement -- returns ElementRef HTMLDivElement -- returns undefined ElementRef -- returns ElementRe ...

getting rid of the angular hash symbol and prefix from the anchor tag

I am having difficulty creating a direct anchor link to a website. Whenever I attempt to link to the ID using: where #20841 is my anchor tag. Angular interferes with the URL and changes it to: This functions properly in Chrome and Firefox, but i ...

Utilizing jQuery UI Slider for Calculating Percentage

I recently worked on an example where I incorporated 5 sliders, which you can see here: Example: http://jsfiddle.net/redsunsoft/caPAb/2/ My Example: http://jsfiddle.net/9azJG/ var sliders = $("#sliders .slider"); var availableTotal = 100; ...

How can I utilize React hook to choose multiple items?

I am currently working on developing a next js application. As part of this project, I have been tasked with creating a custom dropdown select menu using react hooks, specifically useState. The code I have written for this is as follows. Data- export defa ...

The access-control-allow-origin header is failing to receive updates

I need help figuring out how to manually set the Access-origin header. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge& ...

Customizable Mongoose schema attribute

Currently, I am in the process of creating a model using Mongoose that will include a field to store the attributes of my object. The challenge I am facing is that these attributes can vary significantly, for example: StockItem1 : { sku: 23492349, ...

Information is cleared before it is transmitted

Let's begin with the following code: JavaScript: function keyPressed(e) // function for key press event { if (e.keyCode == 13) // 13 represents the enter key { $(this).val(""); } } $(document).ready(function () { $(' ...