cosmo cosmo - 2 months ago 5x
HTML Question

How to keep background color changes on page reload?

I have a table which contain cells that when clicked, will change the cell's background color to green. When I reload the page, however, the background color reverts back to its original color; white. Is there any way I can prevent it from reverting? To just keep it green after I clicked it?


function eight(){
var eight = document.getElementById("eight"); = "green";


<td id="eight" onclick="eight(); ">

*Ignore the content inside the


You have to store the color value in Client side Cookie or Server Side Session. Modern browsers supports localStorage also..

Try this Code..

function changeBackground() {
        if (sessionStorage.getItem('colour')) {
   = sessionStorage.getItem('colour');
   =  "#BB0A21";
            sessionStorage.setItem('colour', "#BB0A21");

  // then you'll need to call your function on page load