Solved

localStorage in forms

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

821 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