Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. <!--Make sure the form has the autocomplete function switched off:-->
  2. <form autocomplete="off" action="/action_page.php">
  3. <div class="autocomplete" style="width:300px;">
  4. <input id="myInput" type="text" name="myCountry" placeholder="Country">
  5. </div>
  6. <input type="submit">
  7. </form>
  8. var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central Arfrican Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];
  9.  
  10.  
  11. * { box-sizing: border-box; }
  12. body {
  13. font: 16px Arial;
  14. }
  15. .autocomplete {
  16. /*the container must be positioned relative:*/
  17. position: relative;
  18. display: inline-block;
  19. }
  20. input {
  21. border: 1px solid transparent;
  22. background-color: #f1f1f1;
  23. padding: 10px;
  24. font-size: 16px;
  25. }
  26. input[type=text] {
  27. background-color: #f1f1f1;
  28. width: 100%;
  29. }
  30. input[type=submit] {
  31. background-color: DodgerBlue;
  32. color: #fff;
  33. }
  34. .autocomplete-items {
  35. position: absolute;
  36. border: 1px solid #d4d4d4;
  37. border-bottom: none;
  38. border-top: none;
  39. z-index: 99;
  40. /*position the autocomplete items to be the same width as the container:*/
  41. top: 100%;
  42. left: 0;
  43. right: 0;
  44. }
  45. .autocomplete-items div {
  46. padding: 10px;
  47. cursor: pointer;
  48. background-color: #fff;
  49. border-bottom: 1px solid #d4d4d4;
  50. }
  51. .autocomplete-items div:hover {
  52. /*when hovering an item:*/
  53. background-color: #e9e9e9;
  54. }
  55. .autocomplete-active {
  56. /*when navigating through the items using the arrow keys:*/
  57. background-color: DodgerBlue !important;
  58. color: #ffffff;
  59. }
  60.  
  61.  
  62. function autocomplete(inp, arr) {
  63. /*the autocomplete function takes two arguments,
  64. the text field element and an array of possible autocompleted values:*/
  65. var currentFocus;
  66. /*execute a function when someone writes in the text field:*/
  67. inp.addEventListener("input", function(e) {
  68. var a, b, i, val = this.value;
  69. /*close any already open lists of autocompleted values*/
  70. closeAllLists();
  71. if (!val) { return false;}
  72. currentFocus = -1;
  73. /*create a DIV element that will contain the items (values):*/
  74. a = document.createElement("DIV");
  75. a.setAttribute("id", this.id + "autocomplete-list");
  76. a.setAttribute("class", "autocomplete-items");
  77. /*append the DIV element as a child of the autocomplete container:*/
  78. this.parentNode.appendChild(a);
  79. /*for each item in the array...*/
  80. for (i = 0; i < arr.length; i++) {
  81. /*check if the item starts with the same letters as the text field value:*/
  82. if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
  83. /*create a DIV element for each matching element:*/
  84. b = document.createElement("DIV");
  85. /*make the matching letters bold:*/
  86. b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
  87. b.innerHTML += arr[i].substr(val.length);
  88. /*insert a input field that will hold the current array item's value:*/
  89. b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
  90. /*execute a function when someone clicks on the item value (DIV element):*/
  91. b.addEventListener("click", function(e) {
  92. /*insert the value for the autocomplete text field:*/
  93. inp.value = this.getElementsByTagName("input")[0].value;
  94. /*close the list of autocompleted values,
  95. (or any other open lists of autocompleted values:*/
  96. closeAllLists();
  97. });
  98. a.appendChild(b);
  99. }
  100. }
  101. });
  102. /*execute a function presses a key on the keyboard:*/
  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. /*If the arrow DOWN key is pressed,
  108. increase the currentFocus variable:*/
  109. currentFocus++;
  110. /*and and make the current item more visible:*/
  111. addActive(x);
  112. } else if (e.keyCode == 38) { //up
  113. /*If the arrow UP key is pressed,
  114. decrease the currentFocus variable:*/
  115. currentFocus--;
  116. /*and and make the current item more visible:*/
  117. addActive(x);
  118. } else if (e.keyCode == 13) {
  119. /*If the ENTER key is pressed, prevent the form from being submitted,*/
  120. e.preventDefault();
  121. if (currentFocus > -1) {
  122. /*and simulate a click on the "active" item:*/
  123. if (x) x[currentFocus].click();
  124. }
  125. }
  126. });
  127. function addActive(x) {
  128. /*a function to classify an item as "active":*/
  129. if (!x) return false;
  130. /*start by removing the "active" class on all items:*/
  131. removeActive(x);
  132. if (currentFocus >= x.length) currentFocus = 0;
  133. if (currentFocus < 0) currentFocus = (x.length - 1);
  134. /*add class "autocomplete-active":*/
  135. x[currentFocus].classList.add("autocomplete-active");
  136. }
  137. function removeActive(x) {
  138. /*a function to remove the "active" class from all autocomplete items:*/
  139. for (var i = 0; i < x.length; i++) {
  140. x[i].classList.remove("autocomplete-active");
  141. }
  142. }
  143. function closeAllLists(elmnt) {
  144. /*close all autocomplete lists in the document,
  145. except the one passed as an argument:*/
  146. var x = document.getElementsByClassName("autocomplete-items");
  147. for (var i = 0; i < x.length; i++) {
  148. if (elmnt != x[i] && elmnt != inp) {
  149. x[i].parentNode.removeChild(x[i]);
  150. }
  151. }
  152. }
  153. /*execute a function when someone clicks in the document:*/
  154. document.addEventListener("click", function (e) {
  155. closeAllLists(e.target);
  156. });
  157. }
  158.  
  159.  
  160.  
  161.  
  162. //Pass the countries array as the second parameter of the autocomplete function:
  163.  
  164. <script>
  165. autocomplete(document.getElementById("myInput"), countries);
  166. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement