• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 195

Backtrace:

File: /home/u125378470/domains/lawhelpguru.org/public_html/application/views/question.php
Line: 195
Function: _error_handler

File: /home/u125378470/domains/lawhelpguru.org/public_html/application/controllers/Questions.php
Line: 416
Function: view

File: /home/u125378470/domains/lawhelpguru.org/public_html/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How do I retain toggle state across website?

So, I have a button that toggles between dark and light mode (my site is dark by default) and it works but now I need it to stay on whatever toggle state is selected across multiple pages. I suspect it has something to do with sessionstorage. Not using jQuery and don't want to. What can I just add to my code to make that happen?

I have five pages all linked to styles.css with an id of "dark" and then in the JS I'm referencing a second style sheet, light.css or 'light' in the JS, so I'm toggling the style sheets. All five pages have the button in the footer each with the same function written in one JS file.

HTML

<head>
<link rel="stylesheet" href="styles.css" id="dark">
</head>

<body>
<button onclick="toggle();" id="light-mode"><i class="fas fa-adjust"></i></button>
</body>

JavaScript

function toggle() {
    var a = document.getElementById("dark");
    a.x = 'light' == a.x ? 'styles' : 'light';
    a.href = a.x + '.css';
}

This works perfectly, just not sure how to integrate sessionstorage or localstorage to save the toggle state across the site. What is the best method proceeding from this point if possible or should I do a complete overhaul?

UPDATE

So, tried @Reality's answer and not working but I think I might just be integrating it wrong. Very new to JS.

localStorage.setItem('toggled','true');

function toggle() {
    var a = document.getElementById("dark");
    a.x = 'light' == a.x ? 'styles' : 'light';
    a.href = a.x + '.css';

    if (localStorage.getItem('toggled') === 'false') {
        localStorage.setItem('toggled','true');
    } else {
        localStorage.setItem('toggled','false');
    }
}

SOLVED

Ok, anyone wanting to know how to toggle a theme perfectly where it works across your website, just follow this: https://dev.to/albertomontalesi/add-dark-mode-to-your-website-with-just-a-few-lines-of-code-5baf

When you follow that, you should do an additional thing to have your theme load better when toggled to. Just link to your second style sheet in the head of every page and add disabled="disabled" If you don't add this to the above approach, I found that when toggling to whatever theme and then navigating around the site, you basically have to load a new style sheet and you get a flicker and can even see the bare HTML version of the site momentarily, so this addition fixes that because by putting it in the head it is already loaded from the beginning.

FINAL

Here's what ended up working:

    <head>
        <link rel="stylesheet" href="dark-theme.css" type="text/css" id="theme">
        <link rel="stylesheet" href="light-theme.css" type="text/css" disabled="disabled">
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                const themeStylesheet = document.getElementById('theme');
                const storedTheme = localStorage.getItem('theme');
                    if (storedTheme) {
                        themeStylesheet.href = storedTheme;
                    }
                const themeToggle = document.getElementById('theme-toggle');
                themeToggle.addEventListener('click', () => {
                    if (themeStylesheet.href.includes('dark')) {
                        themeStylesheet.href = 'light-theme.css';
                    } else {
                        themeStylesheet.href = 'dark-theme.css';
                    }
                    localStorage.setItem('theme', themeStylesheet.href);
                });
            });
        </script>
    </head>

Per @Reality's suggestion, placing the JS in the head improved performance better than what the SOLVED edit suggested doing, however, the code in the link provided (https://dev.to/albertomontalesi/add-dark-mode-to-your-website-with-just-a-few-lines-of-code-5baf) is what is used in the above example. Still needed the disabled="disabled" in the light-theme.css link or else it would override the dark-theme.css and the toggle wouldn't occur all together. This was true of all the code examples seen in this thread that were tried.