localStorage.getItem en setItem werkt niet
Ik ben bezig met een app met een formulier.
De gegevens die zijn ingevoerd moeten worden opgeslagen op local geheugen.
Alleen het werkt niet, want waar de gegevens moeten komen die zijn opgeslagen komt nu "null" te staan.
Dit is de code die ik tot nu heb:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<!-- <script type="text/javascript" charset="utf-8" src="cordova.js"></script>-->
<script>
window.onload= function()
{
document.getElementById("btnStore").addEventListener('click', storeData, false);
document.getElementById("btnLoadDate").addEventListener('click', loadData, false);
}
function storeData()
{
var name= getElementById('name').value;
var email= getElementById('email').value;
localStorage.setItem('name', name);
localStorage.setItem('email', email);
}
function loadData()
{
var nametwee = localStorage.getItem('name');
var emailtwee = localStorage.getItem('email');
alert(nametwee + " " + emailtwee);
}
</script>
<title>home - test</title>
</head>
<body>
<label for="name">Name</label>
<input type="text" id="name" /><br/>
<label for="email">Email</label>
<input type="text" id="email" /><br/>
<button id="btnStore">Store</button><br/>
<button id="btnLoadDate">Load Date</button>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<!-- <script type="text/javascript" charset="utf-8" src="cordova.js"></script>-->
<script>
window.onload= function()
{
document.getElementById("btnStore").addEventListener('click', storeData, false);
document.getElementById("btnLoadDate").addEventListener('click', loadData, false);
}
function storeData()
{
var name= getElementById('name').value;
var email= getElementById('email').value;
localStorage.setItem('name', name);
localStorage.setItem('email', email);
}
function loadData()
{
var nametwee = localStorage.getItem('name');
var emailtwee = localStorage.getItem('email');
alert(nametwee + " " + emailtwee);
}
</script>
<title>home - test</title>
</head>
<body>
<label for="name">Name</label>
<input type="text" id="name" /><br/>
<label for="email">Email</label>
<input type="text" id="email" /><br/>
<button id="btnStore">Store</button><br/>
<button id="btnLoadDate">Load Date</button>
</body>
</html>
Ik werk met phonegap. Alvast bedankt.
Dat moet telkens zijn: document.getElementById
thnks dat is het. Stom van me, ik ben al een hele dag opzoek naar de fout.