auto.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <style>
  6. * {
  7. box-sizing: border-box;
  8. }
  9. /*the container must be positioned relative:*/
  10. .autocomplete {
  11. position: relative;
  12. display: inline-block;
  13. }
  14. input {
  15. border: 1px solid transparent;
  16. background-color: #f1f1f1;
  17. padding: 10px;
  18. font-size: 16px;
  19. }
  20. input[type=text] {
  21. background-color: #f1f1f1;
  22. width: 100%;
  23. }
  24. input[type=submit] {
  25. background-color: DodgerBlue;
  26. color: #fff;
  27. cursor: pointer;
  28. }
  29. .autocomplete-items {
  30. position: absolute;
  31. border: 1px solid #d4d4d4;
  32. border-bottom: none;
  33. border-top: none;
  34. z-index: 99;
  35. /*position the autocomplete items to be the same width as the container:*/
  36. top: 100%;
  37. left: 0;
  38. right: 0;
  39. }
  40. .autocomplete-items div {
  41. padding: 10px;
  42. cursor: pointer;
  43. background-color: #fff;
  44. border-bottom: 1px solid #d4d4d4;
  45. }
  46. /*when hovering an item:*/
  47. .autocomplete-items div:hover {
  48. background-color: #e9e9e9;
  49. }
  50. /*when navigating through the items using the arrow keys:*/
  51. .autocomplete-active {
  52. background-color: DodgerBlue !important;
  53. color: #ffffff;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <h2>Autocomplete</h2>
  59. <p>Start typing:</p>
  60. <!--Make sure the form has the autocomplete function switched off:-->
  61. <form autocomplete="off" action="/action_page.php">
  62. <div class="autocomplete" style="width:300px;">
  63. <input id="myInput" type="text" name="myCountry" placeholder="Country">
  64. </div>
  65. <input type="submit">
  66. </form>
  67. <script>
  68. function autocomplete(inp, arr) {
  69. /*the autocomplete function takes two arguments,
  70. the text field element and an array of possible autocompleted values:*/
  71. var currentFocus;
  72. /*execute a function when someone writes in the text field:*/
  73. inp.addEventListener("input", function(e) {
  74. var a, b, i, val = this.value;
  75. /*close any already open lists of autocompleted values*/
  76. closeAllLists();
  77. if (!val) { return false;}
  78. currentFocus = -1;
  79. /*create a DIV element that will contain the items (values):*/
  80. a = document.createElement("DIV");
  81. a.setAttribute("id", this.id + "autocomplete-list");
  82. a.setAttribute("class", "autocomplete-items");
  83. /*append the DIV element as a child of the autocomplete container:*/
  84. this.parentNode.appendChild(a);
  85. /*for each item in the array...*/
  86. for (i = 0; i < arr.length; i++) {
  87. /*check if the item starts with the same letters as the text field value:*/
  88. if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
  89. /*create a DIV element for each matching element:*/
  90. b = document.createElement("DIV");
  91. /*make the matching letters bold:*/
  92. b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
  93. b.innerHTML += arr[i].substr(val.length);
  94. /*insert a input field that will hold the current array item's value:*/
  95. b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
  96. /*execute a function when someone clicks on the item value (DIV element):*/
  97. b.addEventListener("click", function(e) {
  98. /*insert the value for the autocomplete text field:*/
  99. inp.value = this.getElementsByTagName("input")[0].value;
  100. /*close the list of autocompleted values,
  101. (or any other open lists of autocompleted values:*/
  102. closeAllLists();
  103. });
  104. a.appendChild(b);
  105. }
  106. }
  107. });
  108. /*execute a function presses a key on the keyboard:*/
  109. inp.addEventListener("keydown", function(e) {
  110. var x = document.getElementById(this.id + "autocomplete-list");
  111. if (x) x = x.getElementsByTagName("div");
  112. if (e.keyCode == 40) {
  113. /*If the arrow DOWN key is pressed,
  114. increase the currentFocus variable:*/
  115. currentFocus++;
  116. /*and and make the current item more visible:*/
  117. addActive(x);
  118. } else if (e.keyCode == 38) { //up
  119. /*If the arrow UP key is pressed,
  120. decrease the currentFocus variable:*/
  121. currentFocus--;
  122. /*and and make the current item more visible:*/
  123. addActive(x);
  124. } else if (e.keyCode == 13) {
  125. /*If the ENTER key is pressed, prevent the form from being submitted,*/
  126. e.preventDefault();
  127. if (currentFocus > -1) {
  128. /*and simulate a click on the "active" item:*/
  129. if (x) x[currentFocus].click();
  130. }
  131. }
  132. });
  133. function addActive(x) {
  134. /*a function to classify an item as "active":*/
  135. if (!x) return false;
  136. /*start by removing the "active" class on all items:*/
  137. removeActive(x);
  138. if (currentFocus >= x.length) currentFocus = 0;
  139. if (currentFocus < 0) currentFocus = (x.length - 1);
  140. /*add class "autocomplete-active":*/
  141. x[currentFocus].classList.add("autocomplete-active");
  142. }
  143. function removeActive(x) {
  144. /*a function to remove the "active" class from all autocomplete items:*/
  145. for (var i = 0; i < x.length; i++) {
  146. x[i].classList.remove("autocomplete-active");
  147. }
  148. }
  149. function closeAllLists(elmnt) {
  150. /*close all autocomplete lists in the document,
  151. except the one passed as an argument:*/
  152. var x = document.getElementsByClassName("autocomplete-items");
  153. for (var i = 0; i < x.length; i++) {
  154. if (elmnt != x[i] && elmnt != inp) {
  155. x[i].parentNode.removeChild(x[i]);
  156. }
  157. }
  158. }
  159. /*execute a function when someone clicks in the document:*/
  160. document.addEventListener("click", function (e) {
  161. closeAllLists(e.target);
  162. });
  163. }
  164. /*An array containing all the country names in the world:*/
  165. /*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
  166. autocomplete(document.getElementById("myInput"), countries);
  167. </script>
  168. </body>
  169. </html>