Solved

localStorage in forms

Posted on 2014-11-24
3
148 Views
Last Modified: 2014-11-24
I'm trying to pre-populate a form with a localStorage variable. This code is not working:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script language='JavaScript'>
localStorage.setItem("invitationId", "12345");
document.getElementById("uid").value = localStorage.getItem("invitationId");
</script>

</head>


<body>
      
      <form method="post" name="form1">
            <input type="text" name="uid" id="uid" value="">
            <input type="submit" name="continue" value="continue">
      </form>
                  
</body>
</html>

Any ideas why?
0
Comment
Question by:arielbf
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
It appears that you have to wait until after 'onload' before you can execute 'localStorage'.  I didn't see anywhere that it actually said that but this demo page shows it.  I have two different 'localStorage' variables and only the one that is executed after 'onload' will put the value in the input.
<!DOCTYPE html>
<html>
<head>
<title>localStorage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
function setuid () {
localStorage.setItem("invitationId", "12345");
document.getElementById("uid").value = localStorage.getItem("invitationId");}
</script>
<script type="text/javascript">
<!--
// Save data to the current session's store
localStorage.setItem("username", "John");

// Access some stored data
alert( "username = " + localStorage.getItem("username"));
var usr = localStorage.getItem("invitationId");
document.getElementById("username").value = usr;
// -->
</script>
</head>
<body onload="setuid ();">
<h1>localStorage</h1>
<form action="#" method="get" name="form1">
<input type="text" name="uid" id="uid" value="">
<input type="text" name="username" id="username" value="">
<input type="submit" name="continue" value="continue">
</form>
                  
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:arielbf
Comment Utility
Great, Thank you
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now