Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda