Solved

localStorage in forms

Posted on 2014-11-24
3
155 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 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40461825
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
ID: 40461842
Great, Thank you
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40461845
You're welcome, glad to help.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

832 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