beannshie

Javascript

Nov 4th, 2017
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. function query() {
  2. var x = document.querySelectorAll(".query-p");
  3. var y = document.getElementById("search");
  4. var i;
  5. for(i = 0; i < x.length; i++) {
  6. if(x[i].innerHTML.includes(y.value)) {x[i].style.display = 'block';} else {x[i].style.display = 'none';}
  7. console.log(x[i].innerHTML);
  8. }
  9. }
  10.  
  11. function elm() {
  12. save();
  13. set();
  14. }
  15.  
  16. function remove() {
  17. if(e <= 0 || a <= 0 || h <= 0) {alert("Nothing left to delete.");} else {
  18. e -= 1;
  19. a -= 1;
  20. h -= 1;
  21. localStorage.setItem("e", e);
  22. localStorage.setItem("a", a);
  23. localStorage.setItem("h", h);
  24. location.reload();
  25. }
  26. }
  27.  
  28. function deleteAll() {
  29. localStorage.setItem("e", 0);
  30. localStorage.setItem("a", 0);
  31. localStorage.setItem("h", 0);
  32. location.reload();
  33. }
  34.  
  35. if(localStorage.getItem("e") == null || localStorage.getItem("e") == undefined) {
  36. localStorage.setItem("e", 0);
  37. var e = localStorage.getItem("e");
  38. e = JSON.parse(e);
  39. } else {
  40. console.log(e);
  41. var e = localStorage.getItem("e");
  42. console.log(e);
  43. e = JSON.parse(e);
  44. console.log(e);
  45. }
  46.  
  47. if(localStorage.getItem("h") == null || localStorage.getItem("h") == undefined) {
  48. localStorage.setItem("h", 0);
  49. var h = localStorage.getItem("h");
  50. h = JSON.parse(h);
  51. } else {
  52. var h = localStorage.getItem("h");
  53. h = JSON.parse(h);
  54. }
  55.  
  56. if(localStorage.getItem("s") == null || localStorage.getItem("s") == undefined) {
  57. localStorage.setItem("s", 1);
  58. var s = localStorage.getItem("s");
  59. s = JSON.parse(s);
  60. } else {
  61. var s = localStorage.getItem("s");
  62. s = JSON.parse(s);
  63. }
  64.  
  65. window.onload = function() {
  66. if(e > 0) {
  67. for(h = 1; h <= e; h++) {
  68. var para1 = document.createElement("p");
  69. var para2 = document.createElement("pre");
  70. var element = document.getElementById("test-div");
  71. para1.setAttribute("id", "display-pre"+h);
  72. para1.setAttribute("class", "query-p");
  73. para2.setAttribute("id", "display-pre-a"+h);
  74. //para2.setAttribute("class", "show");
  75. element.insertBefore(para1, document.getElementById("child"));
  76. element.insertBefore(para2, document.getElementById("child"));
  77. document.getElementById("display-pre"+h).innerHTML = localStorage.getItem("display"+h);
  78. document.getElementById("display-pre-a"+h).innerHTML = localStorage.getItem("display-a"+h);
  79. $("#display-pre"+h).attr("data-id", "p"+h);
  80. $("#display-pre-a"+h).attr("data-id", "pre"+h);
  81. localStorage.setItem("h", h);
  82. h = localStorage.getItem("h");
  83. }
  84. }
  85. /*
  86. var p = 1;
  87. setInterval(function() {
  88. if(p <= document.querySelectorAll(".show").length) {
  89. document.getElementById("display-pre"+p).onclick = function() {
  90. console.log(p);
  91. if(document.getElementById("display-pre-a"+p).style.display == '') {document.getElementById("display-pre-a"+p).style.display = 'block';} else
  92. if(document.getElementById("display-pre-a"+p).style.display == 'block') {document.getElementById("display-pre-a"+p).style.display = 'none';}
  93. };
  94. p++;
  95. if(p > document.querySelectorAll(".show").length) {p = 1;}
  96. }
  97. }, 100);*/
  98. }
  99.  
  100. document.onkeydown = checkKey;
  101.  
  102. function checkKey(e) {
  103.  
  104. e = e || window.event;
  105.  
  106. if(e.keyCode == '13') {
  107. query();
  108. }
  109. }
  110.  
  111. document.getElementById("search").value = "";
  112. /*
  113. document.getElementById("search").onblur=function() {
  114. textTransform();
  115. }
  116.  
  117. function textTransform () {
  118. var tgt = document.getElementById("search");
  119. var capitalised = [];
  120. var parts = tgt.value.split(" ");
  121. for (var i=0;i<parts.length;i++) {
  122. capitalised.push(parts[i].substring(0,1).toUpperCase()+parts[i].substring(1));
  123. }
  124. tgt.value=capitalised.join(" ");
  125. }
  126. */
  127. if(localStorage.getItem("a") == null || localStorage.getItem("a") == undefined) {
  128. localStorage.setItem("a", 0);
  129. var a = localStorage.getItem("a");
  130. a = JSON.parse(a);
  131. } else {
  132. var a = localStorage.getItem("a");
  133. a = JSON.parse(a);
  134. }
  135.  
  136. var cre = 0;
  137. function newInput() {
  138. if(cre == 0) {
  139. a++;
  140. var para1 = document.createElement("input");
  141. var para2 = document.createElement("p");
  142. var para3 = document.createElement("button");
  143. var para4 = document.createElement("textarea");
  144. var para5 = document.createElement("pre");
  145. var para6 = document.createElement("br");
  146. var element = document.getElementById("test-div");
  147. para1.setAttribute("id", "textbox"+a);
  148. para2.setAttribute("id", "display-pre"+a);
  149. para2.setAttribute("class", "query-p");
  150. para3.setAttribute("id", "save"+a);
  151. para3.setAttribute("onclick", "elm()");
  152. para4.setAttribute("id", "textarea"+a);
  153. para5.setAttribute("id", "display-pre-a"+a);
  154. para5.setAttribute("class", "show");
  155. element.appendChild(para1);
  156. element.appendChild(para2);
  157. element.appendChild(para3);
  158. element.appendChild(para6);
  159. element.appendChild(para4);
  160. element.appendChild(para5);
  161. document.getElementById("save"+a).innerHTML = "Save";
  162. $(function () {
  163. $("#display-pre"+a).attr("data-id", "p"+a);
  164. $("#display-pre-a"+a).attr("data-id", "pre"+a);
  165. });
  166. cre = 1;
  167. a--;
  168. } else {alert("You already have a new element.");}
  169. }
  170.  
  171. function save() {
  172. a++;
  173. localStorage.setItem("display"+a, document.getElementById("textbox"+a).value);
  174. localStorage.setItem("display-a"+a, document.getElementById("textarea"+a).value);
  175. a--;
  176. }
  177. function set() {
  178. a++;
  179. document.getElementById("textbox"+a).style.display = 'none';
  180. document.getElementById("save"+a).style.display = 'none';
  181. document.getElementById("display-pre"+a).innerHTML = document.getElementById("textbox"+a).value;
  182. document.getElementById("textarea"+a).style.display = 'none';
  183. document.getElementById("display-pre-a"+a).innerHTML = document.getElementById("textarea"+a).value;
  184. cre = 0;
  185. e++;
  186. console.log(e+","+localStorage.getItem("e"));
  187. localStorage.setItem("e", e);
  188. a = localStorage.getItem("e");
  189. a = JSON.parse(e);
  190. console.log(e+","+localStorage.getItem("e"));
  191.  
  192. localStorage.setItem("a", a);
  193. a = localStorage.getItem("a");
  194. a = JSON.parse(a);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment