Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
trying to use local storage to get value JS

i'm trying to make a 'welcome, abby' by clicking submit button but using local storage. Also, it shouldn't be 'welcome, '. it turns out keep refresh after i click the button

<p>
<label for="inName" >What is your name?</label>
<input type="text" id="inName" name="f_name"/>
    </p>

<!-- GET COLOUR-->
<p>
<label for="inColor" >What is your favourite colour? </label>
<input type="color" id="inColor" name="f_color" />
</p>
        
<p>
<input type="submit" id="submit" value="Click to save" />
    </p>

js

var name=document.getElementById("inName");
var backColor=document.getElementById("inColor");
var btn=document.getElementById("submit");
var btndel=document.getElementById("btnDel");

btn.onsubmit=getData;
btndel.onclick=deleteData;

function getData(){
    var temp={
        Name:name.value,
        bckcolor:backColor.value
    }
   
   localStorage.setItem("temp", JSON.stringify(temp));
    
    console.log(localStorage.getItem("temp"));

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

onsubmit is a <form> event, not <input>event.

moreover, if you call both onclick and onsubmit, onclick fires before onsubmit, so you delete data before you get.

about 4 years ago · Juan Pablo Isaza Relatório

0

You'd have to perform the variable assignment operation after the function, like so

function getData(){
    var temp={
        Name:name.value,
        bckcolor:backColor.value
    }
   
   localStorage.setItem("temp", JSON.stringify(temp));
    
    console.log(localStorage.getItem("temp"));

}
btn.onsubmit = getData;

and try it again. JavaScript is executed from the top of the file, to the bottom, so when it reads btn.onsubmit=getData;, it looks above it and says 'I don't see a variable declared called getData, it doesn't exist.' There's many other ways to execute this, of course, but I think that is what you're trying to do. I also do not see the deleteData variable declared anywhere.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda