0

I have used someone else's snippet for a background and color colour change which stores the changes in HTML5 session. This is working great for me.

However, I would really like to elaborate on it slightly, and this is where I am stuck.

Here is an example pen I've done, using the code of my current in-progress site: https://codepen.io/iamfnah/pen/NgoyKy

Notice the body background and color classes toggle, using the checkbox beneath the accordion.

Here is the JS from that example:

    document.body.style.backgroundColor = sessionStorage.getItem('bg');
document.body.style.color = sessionStorage.getItem('cc');
function dark() {
     if ( sessionStorage.getItem('bg') === 'rgb(255, 255, 255)') {

            sessionStorage.setItem('bg', 'rgb(51, 51, 51)');
            sessionStorage.setItem('cc', '#00ff00');
     }
    else if (sessionStorage.getItem('bg') == null || undefined) {
        sessionStorage.setItem('bg', 'rgb(51, 51, 51)');
        sessionStorage.setItem('cc', '#00ff00');

    }
    else if( sessionStorage.getItem('bg') === 'rgb(51, 51, 51)') {

        sessionStorage.setItem('bg', 'rgb(255, 255, 255)');
        sessionStorage.setItem('cc', '#333');
    }
document.body.style.backgroundColor = sessionStorage.getItem('bg');
document.body.style.color = sessionStorage.getItem('cc');
}

There are CSS classes of the accordion, Bootstrap default classes, which I wish to also control the colour changing of, using the same toggle checkbox.

Those are .panel-body and .panel-default>.panel-heading.

So on toggling to the dark background, I want these styles applied;

.panel-body {
  background-color: #1b1b1b;
  color: #ccc;
}
.panel-default>.panel-heading {
  background-color: #373737;
  color: #ccc;
}

I have no idea how you specify such styles within the JS though. I'm making an assumption, that a couple of lines need adding to the top of the JS for the sessionStorage.getItem to define these classes, and then reference them similarly to the bg and cc ones already in the example?

I've tried, but everything I've tried breaks the script from working.

Can anyone please help with the correct JS I need to extend my current script?

Thanks.

1

5 Answers 5

1

The easiest way is to make all your rules depend on a body class:

body.theme1 .panel-body {
  background-color: #1b1b1b;
  color: #ccc;
}
body.theme1 .panel-default > .panel-heading {
  background-color: #373737;
  color: #ccc;
}

Store all your styles in rules like that.

To change the theme, all you need to do is call this:

document.body.className = "theme1";

This is obviously also the only thing you need to store in sessionStorage.

Here's a live example: https://jsfiddle.net/khrismuc/q1o1jmoq/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! appreciate the live example, as I couldn't figure it out myself. I have implemented that into my actual website and it works like a charm now. I like this method too as once your JS has been added, I can easily theme more elements of my site to the dark theme using just CSS, which I am ok with - certainly a better understanding of it than JS anyway. Good stuff
1

Try adding this panel-dark class codepen

.panel-dark .panel-body {
  background-color: #1b1b1b;
  color: #ccc;
}
.panel-dark .panel-default>.panel-heading {
  background-color: #373737;
  color: #ccc;
}

1 Comment

Thanks, this pen you've done is kind of working. It changes the colours which is great. But it doesn't make use of the sessionStorage I think. I've added your code to my website, but when I refresh the page, or come back to the page from another page, the accordion styles revert, and I must click my toggle button again for them to go dark, My other styles though, like the body background, retain. I'm guessing it's something to do with the 'panelDark' not having sessionStorage? Though not sure how best to implement that. Have you any idea how please?
0

getElementsByTagName returns an array of nodes so you must use [] to access elements.

Try

document.getElementsByTagName("body")[0] .style.backgroundColor = sessionStorage.getItem('bg');

Comments

0

Depending on the browser compatibility you want to target (IE10+), you can add/remove/toggle classes with classList

document.body.classList.add('panel-body')
document.body.classList.remove('panel-body')

Comments

0

Both JQuery as well as vanilla JavaScript make this very easy because they allow you to find elements based on standard CSS syntax, so if you are trying to style your accordion (let's say it has an id of accordion and you've previously stored the class names in sessionStorage and retrieved them and that they are now stored in the string variables pb and ph)...

JQuery:

$("#accordion").addClass(pb + " " + ph);
// There is also .removeClass() and .toggleClass()

JavaScript:

document.getElementById("accordion").classList.add(pb, ph);
// There is also .remove() and .toggle()

While you can access an element's className property and supply class names as a space separated list, using classList.add() and classList.remove() is much simpler and less bug prone.

2 Comments

I think toggle is preferred to avoid multiple lines code
@Vikrant Not necessarily, It entirely depends on what you are doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.