Dani Vilardell | d4bfb75 | 2020-09-21 21:32:55 +0200 | [diff] [blame] | 1 | function autocomplete(inp, arr) { |
| 2 | /*the autocomplete function takes two arguments, |
| 3 | the text field element and an array of possible autocompleted values:*/ |
| 4 | var currentFocus; |
| 5 | /*execute a function when someone writes in the text field:*/ |
| 6 | inp.addEventListener("input", function(e) { |
| 7 | var a, b, i, val = this.value; |
| 8 | /*close any already open lists of autocompleted values*/ |
| 9 | closeAllLists(); |
| 10 | if (!val) { return false;} |
| 11 | currentFocus = -1; |
| 12 | /*create a DIV element that will contain the items (values):*/ |
| 13 | a = document.createElement("DIV"); |
| 14 | a.setAttribute("id", this.id + "autocomplete-list"); |
| 15 | a.setAttribute("class", "autocomplete-items"); |
| 16 | /*append the DIV element as a child of the autocomplete container:*/ |
| 17 | this.parentNode.appendChild(a); |
| 18 | /*for each item in the array...*/ |
| 19 | for (i = 0; i < arr.length; i++) { |
| 20 | /*check if the item starts with the same letters as the text field value:*/ |
| 21 | if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) { |
| 22 | /*create a DIV element for each matching element:*/ |
| 23 | b = document.createElement("DIV"); |
| 24 | /*make the matching letters bold:*/ |
| 25 | b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>"; |
| 26 | b.innerHTML += arr[i].substr(val.length); |
| 27 | /*insert a input field that will hold the current array item's value:*/ |
| 28 | b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>"; |
| 29 | /*execute a function when someone clicks on the item value (DIV element):*/ |
| 30 | b.addEventListener("click", function(e) { |
| 31 | /*insert the value for the autocomplete text field:*/ |
| 32 | inp.value = this.getElementsByTagName("input")[0].value; |
| 33 | /*close the list of autocompleted values, |
| 34 | (or any other open lists of autocompleted values:*/ |
| 35 | closeAllLists(); |
| 36 | }); |
| 37 | a.appendChild(b); |
| 38 | } |
| 39 | } |
| 40 | }); |
| 41 | /*execute a function presses a key on the keyboard:*/ |
| 42 | inp.addEventListener("keydown", function(e) { |
| 43 | var x = document.getElementById(this.id + "autocomplete-list"); |
| 44 | if (x) x = x.getElementsByTagName("div"); |
| 45 | if (e.keyCode == 40) { |
| 46 | /*If the arrow DOWN key is pressed, |
| 47 | increase the currentFocus variable:*/ |
| 48 | currentFocus++; |
| 49 | /*and and make the current item more visible:*/ |
| 50 | addActive(x); |
| 51 | } else if (e.keyCode == 38) { //up |
| 52 | /*If the arrow UP key is pressed, |
| 53 | decrease the currentFocus variable:*/ |
| 54 | currentFocus--; |
| 55 | /*and and make the current item more visible:*/ |
| 56 | addActive(x); |
| 57 | } else if (e.keyCode == 13) { |
| 58 | /*If the ENTER key is pressed, prevent the form from being submitted,*/ |
| 59 | e.preventDefault(); |
| 60 | if (currentFocus > -1) { |
| 61 | /*and simulate a click on the "active" item:*/ |
| 62 | if (x) x[currentFocus].click(); |
| 63 | } |
| 64 | } |
| 65 | }); |
| 66 | function addActive(x) { |
| 67 | /*a function to classify an item as "active":*/ |
| 68 | if (!x) return false; |
| 69 | /*start by removing the "active" class on all items:*/ |
| 70 | removeActive(x); |
| 71 | if (currentFocus >= x.length) currentFocus = 0; |
| 72 | if (currentFocus < 0) currentFocus = (x.length - 1); |
| 73 | /*add class "autocomplete-active":*/ |
| 74 | x[currentFocus].classList.add("autocomplete-active"); |
| 75 | } |
| 76 | function removeActive(x) { |
| 77 | /*a function to remove the "active" class from all autocomplete items:*/ |
| 78 | for (var i = 0; i < x.length; i++) { |
| 79 | x[i].classList.remove("autocomplete-active"); |
| 80 | } |
| 81 | } |
| 82 | function closeAllLists(elmnt) { |
| 83 | /*close all autocomplete lists in the document, |
| 84 | except the one passed as an argument:*/ |
| 85 | var x = document.getElementsByClassName("autocomplete-items"); |
| 86 | for (var i = 0; i < x.length; i++) { |
| 87 | if (elmnt != x[i] && elmnt != inp) { |
| 88 | x[i].parentNode.removeChild(x[i]); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | /*execute a function when someone clicks in the document:*/ |
| 93 | document.addEventListener("click", function (e) { |
| 94 | closeAllLists(e.target); |
| 95 | }); |
| 96 | } |