Guest User

Untitled

a guest
Jan 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. var myButton = document.getElementById('button');
  2. var clearButton = document.getElementById('clear');
  3. var cookies = document.querySelector('cookies');
  4. var pw = document.querySelector('pw');
  5. var inp = document.getElementById('myInput');
  6. var inpw = document.getElementById('myPW');
  7.  
  8. var inputData = [];
  9. var inputPW = [];
  10.  
  11. let itemsArray = localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')) : [];
  12. let itemsArray2 = localStorage.getItem('items2') ? JSON.parse(localStorage.getItem('items2')) : [];
  13.  
  14. localStorage.setItem('items', JSON.stringify(itemsArray));
  15. data = JSON.parse(localStorage.getItem('items'));
  16.  
  17. localStorage.setItem('items2', JSON.stringify(itemsArray2));
  18. data2 = JSON.parse(localStorage.getItem('items2'));
  19.  
  20. liMaker = (text) => {
  21. li = document.createElement('li');
  22. li.textContent = text;
  23. cookies.appendChild(li);
  24.  
  25. inputData.push(text);
  26. }
  27.  
  28. liMaker2 = (text2) => {
  29. li2 = document.createElement('li');
  30. li2.textContent = text2;
  31. pw.appendChild(li2);
  32.  
  33. inputPW.push(text2);
  34. }
  35.  
  36. myButton.addEventListener('click', function(event) {
  37. event.preventDefault();
  38.  
  39. itemsArray.push(inp.value);
  40. localStorage.setItem('items', JSON.stringify(itemsArray));
  41. liMaker(inp.value);
  42. inp.value = "";
  43.  
  44. itemsArray2.push(inpw.value);
  45. localStorage.setItem('items2', JSON.stringify(itemsArray2));
  46. liMaker2(inpw.value);
  47. inpw.value = "";
  48.  
  49. handleData();
  50. });
  51.  
  52. data.forEach(myInput => {
  53. liMaker(myInput);
  54. });
  55.  
  56. data2.forEach(myPW => {
  57. liMaker2(myPW);
  58. });
  59.  
  60. function handleData() {
  61. alert(itemsArray[0]);
  62. alert(inputData[0]);
  63. }
  64.  
  65. clearButton.addEventListener('click', function () {
  66. localStorage.clear();
  67. while (cookies.firstChild && pw.firstChild) {
  68. cookies.removeChild(cookies.firstChild);
  69. pw.removeChild(pw.firstChild);
  70. }
  71. itemsArray = [];
  72. itemsArray2 = [];
  73. });
  74.  
  75. //------------------------------------------
  76.  
  77. function autocomplete(inp, arr) {
  78. var currentFocus;
  79. inp.addEventListener("input", function(e) {
  80. var a, b, i, val = this.value;
  81. closeAllLists();
  82. if (!val) { return false;}
  83. currentFocus = -1;
  84. a = document.createElement("DIV");
  85. a.setAttribute("id", this.id + "autocomplete-list");
  86. a.setAttribute("class", "autocomplete-items");
  87. this.parentNode.appendChild(a);
  88. for (i = 0; i < arr.length; i++) {
  89. if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
  90. b = document.createElement("DIV");
  91. b.innerHTML = arr[i].substr(0, val.length);
  92. b.innerHTML += arr[i].substr(val.length);
  93. b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
  94. b.addEventListener("click", function(e) {
  95. inp.value = this.getElementsByTagName("input")[0].value;
  96. myPW.value = pass[inp.value];
  97. closeAllLists();
  98. });
  99. a.appendChild(b);
  100. }
  101. }
  102. });
  103. inp.addEventListener("keydown", function(e) {
  104. var x = document.getElementById(this.id + "autocomplete-list");
  105. if (x) x = x.getElementsByTagName("div");
  106. if (e.keyCode == 40) {
  107. currentFocus++;
  108. addActive(x);
  109. } else if (e.keyCode == 38) {
  110. currentFocus--;
  111. addActive(x);
  112. } else if (e.keyCode == 13) {
  113. e.preventDefault();
  114. if (currentFocus > -1) {
  115. if (x) x[currentFocus].click();
  116. }
  117. }
  118. });
  119. function addActive(x) {
  120. if (!x) return false;
  121. removeActive(x);
  122. if (currentFocus >= x.length) currentFocus = 0;
  123. if (currentFocus < 0) currentFocus = (x.length - 1);
  124. x[currentFocus].classList.add("autocomplete-active");
  125. }
  126. function removeActive(x) {
  127. for (var i = 0; i < x.length; i++) {
  128. x[i].classList.remove("autocomplete-active");
  129. }
  130. }
  131. function closeAllLists(elmnt) {
  132. var x = document.getElementsByClassName("autocomplete-items");
  133. for (var i = 0; i < x.length; i++) {
  134. if (elmnt != x[i] && elmnt != inp) {
  135. x[i].parentNode.removeChild(x[i]);
  136. }
  137. }
  138. }
  139. document.addEventListener("click", function (e) {
  140. closeAllLists(e.target);
  141. });
  142. }
  143.  
  144. //DATA OF inputData[i]: inputPW[i]; i++ NEED TO BE PASSED TO THIS STORAGE
  145. //i IS INCREMENTING AS PER HOW MANY THE USER'S INPUT
  146. var pass = {
  147. //ARRAY HERE OF inputData[i]: inputPW[i]
  148. };
  149.  
  150. autocomplete(document.getElementById("myInput"), itemsArray); //it can also be inputData
  151.  
  152. document.getElementById("button").addEventListener("click", clickFunction);
Add Comment
Please, Sign In to add comment