Guest User

Untitled

a guest
Aug 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Javascript + DOM</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  9.  
  10. <title>Document</title>
  11. </head>
  12. <body>
  13. <h1>Shopping list</h1>
  14. <br>
  15. <input id="userInput" placeholder="enter item" type="text">
  16. <button id="button">enter</button>
  17. <br>
  18.  
  19. <ul class="list">
  20. </ul>
  21.  
  22. </body>
  23. <script src="script.js"></script>
  24. </html>
  25.  
  26. var input = document.getElementById('userInput');
  27. var button = document.getElementById('button');
  28. var ul = document.querySelector('ul');
  29. var li = document.getElementsByClassName("item");
  30.  
  31. // strikethrough task on click
  32. function strike() {
  33. for(var i = 0; i < li.length; i++)
  34. {
  35. li[i].addEventListener('click',function() {
  36. if(this.classList.contains("iteJm")) {
  37. this.classList.toggle("done");
  38. }
  39. });
  40. }
  41. }
  42.  
  43. function inputLength() {
  44. return input.value.length;
  45. }
  46.  
  47. function createListElement() {
  48. var li = document.createElement("li");
  49. li.appendChild(document.createTextNode(input.value));
  50. ul.appendChild(li);
  51. li.classList.add('item');
  52. input.value = '';
  53. strike();
  54. }
  55.  
  56. function addToListAfterClick() {
  57. if(inputLength() > 0) {
  58. createListElement();
  59. }
  60. }
  61.  
  62. function addToListAfterEnter(event) {
  63. if(inputLength() > 0 && event.keyCode == 13) {
  64. createListElement();
  65.  
  66. }
  67. }
  68.  
  69. button.addEventListener('click',addToListAfterClick);
  70. input.addEventListener('keypress', addToListAfterEnter);
  71.  
  72. .done {
  73. text-decoration: line-through;
  74. }
  75.  
  76. li.addEventListener('click', strike);
Add Comment
Please, Sign In to add comment