JavaScript: The initial function call does not correctly switch the "boolean" in Local Storage

I'm currently developing a project that involves implementing a tutorial overlay. My goal is to have the overlay toggle on and off using a button, while also ensuring that the state of the button is remembered between page transitions so that users don't have to hide the overlay every time they switch pages. Below is the code I am working with:

<!-- The two functions responsible for toggling the overlay -->
<script>
        // This function changes the class of the overlay div and updates the Local Storage "boolean" value
        function toggleOverlayVisibility(){
            document.getElementById('overlay').classList.toggle('hidden');
            sessionStorage.setItem('overlayVisibilityState', String(document.getElementById('overlay').classList.contains("hidden")));
        };
        // this function runs when the user switches pages and ensures that the overlay is hidden or visible accordingly.
        onload = function() {
            if(sessionStorage.getItem('overlayVisibilityState') == "true"){
               document.getElementById('overlay').classList.remove("hidden");
            } else if(sessionStorage.getItem('overlayVisibilityState') == "false") {
               document.getElementById('overlay').classList.add("hidden");
            } else {
               sessionStorage.setItem('overlayVisibilityState', "true");
               document.getElementById('overlay').classList.remove("hidden");
            }
 </script>

 <!-- the button used to toggle the overlay -->
 <a href="#" onclick="toggleOverlayVisibility();"><img src="/static/icons/overlay.jpg" class="info-overlay"></a>

 <!-- the overlay div that gets toggled -->
 <div id="overlay" class="hidden">
        <!-- the overlay is divided into 4 sections for proper display -->
        <p style="background-color:white; position:absolute; top:0; left:0; width:100%; height:100%"></p>
        <img src="/static/overlays/main-upper-left.png" style="position:absolute; top:0;">
        <img src="/static/overlays/main-lower-left.png" style="position:absolute; bottom:50px; left:-30px;">
        <img src="/static/overlays/main-upper-right.png" style="position:absolute; top:0; right:0;">
 </div>

Initially, when the program loads, there is no "overlayVisibilityState" in Local Storage. Therefore, the onload function removes the hidden attribute to display the overlay and sets Local Storage accordingly.

Upon switching pages, the overlay will be displayed on each page as expected.

The intriguing part comes in when the user interacts with the overlay button. Toggling the button will hide or show the overlay by adding or removing the "hidden" attribute to the div. However, the Local Storage does not update along with it.

As a result, upon subsequent page switches, the overlay continues to display despite being manually turned off earlier.

If the user iterates pressing the overlay button multiple times, the overlay will cycle through toggles correctly but Local Storage only updates once. This causes the overlay Boolean to become out of sync!

To simplify things, consider this pseudocode conditional explanation:

if single toggle
     overlay switches = 1
     Local History switches = 0
if multiple toggles
     overlay switches = number of toggles
     Local History switches = number of toggles - 1

My main concern is why doesn't Local Storage toggle along with the first button press? Feel free to reach out if you need further clarification or more code samples.

Answer №1

 sessionStorage.setItem('overlayVisibilityState',
   String(document.getElementById('overlay').classList.contains("hidden")));

stores the visibility state of the overlay in the session storage as a string, based on whether it is hidden or not.

However, the current logic

if(sessionStorage.getItem('overlayVisibilityState') == "true"){
   document.getElementById('overlay').classList.remove("hidden");
} else if(sessionStorage.getItem('overlayVisibilityState') == "false") {
   document.getElementById('overlay').classList.add("hidden");
} else {
   sessionStorage.setItem('overlayVisibilityState', "true");
   document.getElementById('overlay').classList.remove("hidden");
}

interprets "true" to mean that the overlay is not hidden.

To address this issue, consider using a complement operation when setting the value:

sessionStorage.setItem('overlayVisibilityState',
 !document.getElementById('overlay').classList.contains("hidden"));

(The setItem method should automatically convert the boolean value to a string.)

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

Construct a connection between divs

Which element is necessary to generate black lines within the image ? [![enter image description here][1]][1] I have already coded but I'm seeking to produce lines similar to the image (between images). What's the process for achieving this? Kin ...

Eliminating specific text and images from WordPress articles

I am working on a Wordpress website where the post's content is displayed using <?php the_content(); ?> The content includes images and text that are all outputted within < p > tags. My goal is to style the margins/padding of the image ...

JavaScript label value vanishing after postback

When using a datepicker in JavaScript, I encountered an issue where the label resets to the default value after a postback to the server, instead of retaining the user's selected values. Despite my attempts to rearrange the code, the label consistent ...

When data is returned empty, DataTable does not get initialized

Our DataTable setup is dynamic, initializing even when the data is empty and waiting to be filled in once a specific option is selected. Recently, we made updates to jQuery version 3.7 and DataTable v2.0.6. Now, the table seems to be stuck in a loading pr ...

What is the process for exporting data from MongoDB?

Recently, I created a web application for fun using Handlebars, Express, and Mongoose/MongoDB. This app allows users to sign up, post advertisements for others to view and respond to. All the ads are displayed on the index page, making it a shared experie ...

NodeJS npm module installed globally is unable to run the main/bin JavaScript file using node command

Here is an example of my package.json: { "name": "example-module", "version": "1.0.0", "bin": "./bin/example-module.js", "main": "./bin/example-module.js", "description": "Example module description", "homepage": "http://my.home.page.com", " ...

Design a logo to serve as the main button on the navigation bar of the

I've been experimenting with adding a logo instead of the 'Home' text in my navigation bar, but I'm not sure where to implement it. Should I add it within #nav or separately? Also, I want the nav bar to stay fixed without overlapping on ...

The functionality of the Toastr "options" seems to be malfunctioning

I am having some trouble with my Toastr notification messages. While the message does display, I seem to be unable to make use of any options that I set. Despite specifying some options, they do not seem to work as intended. $('#editButton').c ...

perform asynchronous calls within a for loop in a synchronous manner

Having issues with managing asynchronous events in sails.js. Obtaining data from a JSONapi and attempting to insert it into the database sequentially within a for loop. The goal is to maintain the correct order of execution. For simplicity, consider the f ...

Switch the div class when clicked, and revert it when the body is clicked

Allow me to elaborate on the issue: I currently have <div class="contact"> <div id="form"></div> <div id="icon"></div> </div> My goal is to change the class of .contact to .contactexpand (or simply ...

Show the initial three divs first before implementing a toggle for the rest

My dashboard consists of multiple panels, each containing various items. Some panels have a large number of items. I am looking to display only the first three items in each panel and provide a toggle option to show/hide the rest. <div class="dashboa ...

"Utilizing a unique jQuery grid numbering system for organizing and categorizing each

I am attempting to implement a numbering system for elements that mimics a grid layout, similar to an Excel spreadsheet. Within a container, my elements are structured like this: <div class="container"> <div class="ele"></div> & ...

Looking for a discreet JavaScript-based text editor with advanced features?

Our CMS has been using the now-unsupported RichTextBox control for a while, and we want to find a lighter-weight alternative with better cross-browser support. Initially, we were considering different ASP.NET components, but I am starting to think that an ...

Acquiring row data upon checkbox selection: A step-by-step guide

I'm struggling to separate and assign the values returned by a function to different parameters. function saveTaxes() { $('input[type=checkbox]').each(function() { if ($(this).is(':checked')) { //test console.log ...

Can CSS alone be used to provide a cover for auto-scaling images?

One common technique is using max-height and max-width to make an image automatically fit in a div, as shown below: .cover img { max-width: 100%; max-height: 100%; } But is there a way to create a translucent cover (with opacity set to 0.8) that ...

The functionality of AJAX is triggered only on the second click

On my index.html page, I'm attempting to implement a basic AJAX functionality. When clicking on an anchor tag, the text will be sent to a PHP page called data.php, which will then display the clicked page. The code is functional, but there seems to be ...

What is the best way to ensure an observable has been updated before proceeding with additional code execution?

Is an observable the best choice for providing live updates of a variable's value to another class? I have a loop in my service class that looks like this: elements.forEach(element => { doStuff(); this.numberSubject.next(valueFromDoStuff); }) ...

When you hover over the button, it seamlessly transitions to a

Previously, my button component was styled like this and it functioned properly: <Button component={Link} to={link} style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', height: 48, padding: '0 ...

Exploring the process of fetching and showcasing API responses through AJAX

I need to pass the value of my car to an API, which is retrieved from the HTML below. After that, I would like to send this value to the API using AJAX. Finally, I want to display all the models in the same way as I displayed the cars. $(document).r ...

Issues observed with the functionality of the fixed position menu bar

Is it strange that the menu bar remains fixed at the top of the page while scrolling down, but ends up going under another div making it impossible to click on the menu? #cssmenu { position: fixed; left: 0; top: 0; height: 40px; width: 100%; ...