Guest User

Untitled

a guest
Nov 21st, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>HTML5 – Web Storage - List</title>
  5. <script type="text/javascript">
  6. //add values to list
  7. function addToList()
  8. {
  9. var val = document.getElementById("textData").value;
  10. if(val.length > 0){
  11. localStorage.setItem("html5List" + (localStorage.length), val);
  12. showList();
  13. }
  14. }
  15.  
  16. //clear the list
  17. function clearList()
  18. {
  19. localStorage.clear();
  20. showList();
  21. }
  22.  
  23. //display the list
  24. function showList()
  25. {
  26. if (typeof(localStorage) == 'undefined') {
  27. document.write("Your Browser Does Not Support localStorage");
  28. }
  29. else{
  30. document.getElementById("textData").value = "";
  31. var listData = "<h4>";
  32. listData += "List has " + localStorage.length + " item(s) <br/>";
  33. for(var i=0; i<localStorage.length; i++){
  34. var keyStr = "html5List"+i;
  35. listData += "KEY: " + keyStr +
  36. " VALUE: " + localStorage.getItem(keyStr) + "<br/>" ;
  37. }
  38. listData += "</h4>";
  39. document.getElementById("theList").innerHTML = listData;
  40. }
  41. }
  42. </script>
  43. </head>
  44. <body onLoad="showList();">
  45. <form>
  46. Enter Value: <input type="text" id="textData"/>
  47. <input type="button" onClick="addToList()" value="Add To List"/>
  48. <input type="button" onClick="clearList()" value="Clear List"/>
  49. </form><br/>
  50. <div style="border:2px solid gray;width:500px;padding-left:10px">
  51. <h2 style="color:#0a6900;text-decoration:underline;"> The List </h2>
  52. <div id="theList"></div>
  53. </div>
  54. </body>
  55. </html>
Add Comment
Please, Sign In to add comment