Solved

localStorage in forms

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does Redux fit into React? 3 70
Allow space in this pattern 2 48
JS Plugin 4 29
How to hide the rows in Table when the value is empty ? 4 19
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now