After success creating an iBooks Author HTML5 widget that saves user input via text boxes, I thought I'd have a go at another.
What I want is to create a checklist widget that
(a) saves the user's ticked checkboxes in local storage
(
I'm new to javascript, but ok in VB. My thinking was to
1. Create an array and manually populate it in javascript
2. save() function - loop through the array and save those checkboxes that have been ticked
3. load() function - loop through the array and display ticks in the checkboxes that had previously been checked
My code so far has css, javascript and html in the same file - checklist.html
<!DOCTYPE HTML>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<title>Year 9 ICT Website Design</title>
<style>
body{
font-family: Helvetica, Arial, Verdana, sans-serif;
padding: 3px;
margin: 3px;
}
ul{
list-style: none;
padding: 3px;
margin: 3px;
}
li{
clear: left;
font-size: 30px;
padding: 6px 6px 6px 20px;
margin: 6px;
}
li input{
float: left;
margin-right: 30px;
width: 50px;
height: 30px;
}
li:nth-child(even){
background-color: #eee;
}
h1{
font-size: 50px;
padding: 0.4em, 1em;
border-bottom: 2px solid #999;
background-color: #eee;
margin: 3px;
}
</style>
<script>
var checklistItems = new Array();
checklistItems[0] = "chkBox1";
checklistItems[1] = "chkBox2";
checklistItems[2] = "chkBox3";
function save() {
for (var i = 0; i < checklistItems.length; i++) {
if(checklistItems[i].checked) {
localStorage.setItem("checkedItems", JSON.stringify(checklistItems[i]));
}
}
alert("Data saved!");
}
function load() {
for (var i = 0; i < checklistItems.length; i++) {
if(checklistItems[i].checked) {
localStorage.getItem("checkedItems");
}
}
}
</script>
</head>
<body onload="load()">
<h1>Website Project Checklist</h1>
<ul>
<li><input type="checkbox" name="chkBox1"/>Item One</li>
<li><input type="checkbox" name="chkBox2"/>Item Two</li>
<li><input type="checkbox" name="chkBox3"/>Item Three</li>
<p><input type="submit" name="btnSave" value="Save Checklist" onclick="save()"/></p>
</ul>
</body>
</html>
I know things aren't right because it's not working. Data doesn't persist when the widget is reloaded after moving 2 pages away or reloading the app. I'm guessing it's both the save() and load() functions, but after much searching online, I'm not sure what to do next.
Can anyone point me in the right direction?
This post has been edited by south73paw: 11 December 2012 - 02:09 PM

New Topic/Question
Reply



MultiQuote





|