5
I am developing a website and wanted to implement that famous "dark" function so that the user can choose.
Searching and mining our dear internet, I managed to have a certain result, I created the button "Dark" and breaking the "Ligth".
However, every time the page of refresh the site returns to the pattern (same white). My wish is to fix this function so that it is no longer altered. I’d like a little help from you masters!
Follow the code of what is already ready:
HTML
<body id=“bg”>
…
div>
<button onclick=“light()” type=“button” >Tema Light</button>
<button onclick=“dark()” type=“button” >Tema Dark</button>
</div>
Javascript
function dark()
{
document.getElementById(‘bg’).style.background=’#212121 center center no-repeat’;
}
function light()
{
document.getElementById(‘bg’).style.background=’#e6e6e6 center center no-repeat’;
}
You will have to store the information in a cookie or in your database and set when loading the page in question
– Victor Laio
I didn’t understand the use of
id
on the tagbody
. You’ll get more than onebody
on the page?– Sam
Possible duplicate of Change the style/theme of the site and store it in Localstorage
– Woss