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

160
Visualizações
How to get data from Local storage onClick in javascript

How to use the data from local storage in javascript onClick. I have my html file and javascript file below. Thanks

            <fieldset>
             <form>
               <!--GET USER NAME-->
            <p>
                <label for="inName" >What is your name?</label>
                <input type="text" id="inName" name="f_name"/>
            </p>
        
            <!-- GET COLOUR--> // I m not sure about how the get color as user can 
                               //choose the color from drop down color palet.
            <p>
                <label for="inColor" >What is your favourite colour? </label>
                <input type="color" id="inColor" name="f_color" />
            </p>
            
            <p> // how to get the value when user clicks the button
                // in order to call onClick function to output the 
                  user's name and 
                // display favorite color in background.  
                <input type="submit" value="Click to save" />
            <p/>
            </fieldset>
                     </form>
        

I don't know how to get data from local storage when user click to submit the form :

//NOW THAT WE HAVE STORED DATA ON ONE PAGE, WE CAN ACCESS IT FROM ANY PAGE ON THIS 
  WEBSITE.
window.onload = function(){     
//GET ELEMENTS USED FOR OUTPUT
    var userOut = document.getElementById("newMsgBox");
//GET VALUES FROM COOKIES/LOCAL STORAGE
    var userName = localStorage.getItem("nameIn");
    var userColor = localStorage.getItem("inColor");
//CREATE OUTPUT WITH VALUES
if (userName !== null) {
        userOut.innerHTML = " " +userName;
    }   
}//end onload
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you want to listener event from an specific button and then access to your localStorage you could do this:

first get the element:

const button = document.getElementById('<clickable_element_id>');

then add a listener to the button;

button.addEventListener("click",(event)=> {
  event.preventDefault() // only if you want to prevent the action

  //here you can access to your local store items like:
  const a = localStorage.getItem("<item you saved previously>");
  ...
});
about 4 years ago · Santiago Trujillo Relatório

0

It's really unclear what you want to do but

let inColor = document.getElementById('inColor');
let inName = document.getElementById('inColor');

Will fetch the values for you. I think you are looking for something like this.

<script>
function saveData() {
  let inColor = document.getElementById('inColor');
  let inName = document.getElementById('inName');
  localStorage.setItem("inColor", inColor);
  localStorage.setItem("inName", inName);     
}
</script>
<input type="Button" value="Click to save" onclick="saveData"/>
about 4 years ago · Santiago Trujillo Relatório

0

Import Jquery and use this. I hope help you. when you click on button with id idOfBtn jquery gonna eject the event and call the localStorage with key (key). Replace it for you key name.

    $("#idOfBtn").click(function() {
     alert(localStorage.getItem('_key_'));
    });
about 4 years ago · Santiago Trujillo 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