Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!