Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <meta charset="utf-8" />
  5. </head>
  6.  
  7. <body>
  8. <h1>Einkaufsliste</h1>
  9.  
  10. <button onclick="addNewItem(); updateList();">Neuen Eintrag hinzufügen</button>
  11.  
  12. <ul id="list">
  13.  
  14. </ul>
  15.  
  16. <script>
  17.  
  18. let shoppingItems = getShoppingItemsFromLocalStorage();
  19.  
  20. updateList();
  21.  
  22. function getShoppingItemsFromLocalStorage() {
  23. let items = localStorage.getItem('shoppingItems');
  24.  
  25. if (items == null || items == '') {
  26. items = [];
  27. } else {
  28. items = items.split(',');
  29. }
  30.  
  31. return items;
  32. }
  33.  
  34. function addNewItem() {
  35. let item = prompt('Was möchtest du hinzufügen?');
  36.  
  37. if (item != null) {
  38. shoppingItems.push(item);
  39. localStorage.setItem('shoppingItems', shoppingItems);
  40. }
  41. }
  42.  
  43. function removeItem(itemIndex) {
  44. shoppingItems.splice(itemIndex, 1);
  45. localStorage.setItem('shoppingItems', shoppingItems);
  46. }
  47.  
  48. function updateList() {
  49. document.getElementById('list').innerHTML = '';
  50.  
  51. for (let index = 0; index < shoppingItems.length; index += 1) {
  52. document.getElementById('list').innerHTML += '<li>' + shoppingItems[index] +
  53. ' <button onclick="removeItem(' + index + '); updateList();">X</button></li>';
  54. }
  55. }
  56. </script>
  57. </body>
  58.  
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement