Solved

localStorage in forms

Posted on 2014-11-24
3
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Suggested Courses

740 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