1
I was able to make a code that changes the color of my navbar with HTML and Javascript, but when the person updates the navbar color back to normal, how can I use localStorage to save this data, until the user changes the navbar color again? Follow the button code I created:
<div class="container p-4">
<h1>Selecione uma cor:</h1>
<button type="button" name="button" class="btn btn-primary col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'blue'">Azul</button>
<button type="button" name="button" class="btn btn-success col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'green'">Verde</button>
<button type="button" name="button" class="btn btn-dark col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'black'">Preto</button>
<button type="button" name="button" class="btn btn-warning col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'orange'">Laranja</button>
<button type="button" name="button" class="btn btn-secondary col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'grey'">Cinza</button>
<button type="button" name="button" class="btn btn-danger col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" onclick="document.getElementById('navbar').style.background = 'red'">Vermelho</button>
</div>
My question is, how do I save this data from the button with localStorage and how do I load this data from localStorage on my system?
Sam, thank you so much, you have fully clarified my doubt! but I go further, if I wish to do this with a div as well, I would include something here: Document.getElementById('navbar').style.background = c; or would I have to do otherwise? to apply the color on the navbar and on a div
– Fábio Stralioti Martins
Blz, amigo. Learn how to thank on [tour]. Abs!
– Sam
I already gave the vote, if you can help me with the doubt I put on top, I’d appreciate it
– Fábio Stralioti Martins
You can take it by the id of the div, if you have it. Something like:
document.getElementById('id da div').style.background = c;
– Sam
I get it, but would I put down Document.getElementById('navbar').style.background = c; ? in case it would be Document.getElementById('navbar').style.background = c; Document.getElementById('minhadiv').style.background = c;
– Fábio Stralioti Martins
Yeah, just put it under the other.
– Sam
I tried to go to the chat but the stack over flow, it didn’t permet, good. i put it this way: Document.getElementById('navbar').style.background = ls; Document.getElementById('navbar-left').style.background = ls; but it does not activate, so I noticed, even taking the top navar it does not activate the color on the side, Can’t you find the ID? my Section is like this <Section class="Nav-menu-left" id="navbar-left">
– Fábio Stralioti Martins
Try through the console. Go there and run
document.getElementById('navbar-left').style.background = "blue"
to see if you change the color. Or trybackgroundColor
in place ofbackground
. Could be some conflict.– Sam
is he didn’t set the color, by the console too, I think it’s some conflict, or this code only works with a background?
– Fábio Stralioti Martins
Go to inspect browser elements and see if the background property has been applied and is scratched. If scratched, there is something (probably in CSS) ignoring the background.
– Sam
he is not ignoring, actually not even applying, there is another technique to use other than with ID?
– Fábio Stralioti Martins
By class:
document.querySelector(".nav-menu-left")
– Sam
I don’t know if you will read or be able to help me, the code worked perfectly, but today I have a question I can change the <meta name="Theme-color" content="" /> using javascript using the same button?
– Fábio Stralioti Martins