How to use localStorage in this case?

Asked

Viewed 68 times

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?

1 answer

2


Instead of using the attribute onclick on each of the buttons, create a Event Handler onclick in all of them and put in each one a dataset with the name of the color. For example:

data-cor="blue"

This attribute will be searched in the event and saved in the localStorage. When you reload the page, just check if localStorage exists and apply the color saved in the navbar.

Buttons should look like this below. Note the attribute data-cor in each:

<button type="button" name="button" class="btn btn-primary col-sm-4 col-md-4 col-lg-4 pt-4 mt-4" data-cor="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" data-cor="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" data-cor="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" data-cor="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" data-cor="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" data-cor="red">Vermelho</button>

And Javascript will look like this:

document.addEventListener("DOMContentLoaded", function(){
   // seleciona todos os botões pelo atributo "name"
   var bts = document.querySelectorAll("[name=button]");
   // percorre todos os botões
   for(var botao of bts){
      // cria o event handler
      botao.onclick = function(){
         // pega o valor do dataset
         var c = this.dataset.cor;
         // aplica no navbar
         document.getElementById('navbar').style.background = c;
         // salva a cor no LS
         localStorage.setItem("cor", c);
      }
   }

   // aqui verifica o localStorage
   var ls = localStorage.getItem("cor");
   if(ls){
      document.getElementById('navbar').style.background = ls;
   }

});

Obs.: the script will only work if only these buttons have the name "button". If you have another button outside that collection with the same name and it is clicked, will bug the localStorage.

  • 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

  • Blz, amigo. Learn how to thank on [tour]. Abs!

  • I already gave the vote, if you can help me with the doubt I put on top, I’d appreciate it

  • You can take it by the id of the div, if you have it. Something like: document.getElementById('id da div').style.background = c;

  • 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;

  • Yeah, just put it under the other.

  • 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">

  • Try through the console. Go there and run document.getElementById('navbar-left').style.background = "blue" to see if you change the color. Or try backgroundColor in place of background. Could be some conflict.

  • 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?

  • 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.

  • he is not ignoring, actually not even applying, there is another technique to use other than with ID?

  • By class: document.querySelector(".nav-menu-left")

  • 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?

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.