This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoForm.js
74 lines (71 loc) · 3.38 KB
/
autoForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function autocomplete(inp, arr) { //DOM element, autoCmpltArray
var currentFocus;
inp.addEventListener("input", function(e) { //onInput
var a, b, i, val = this.value;
closeAllLists(); //close opened
if (!val) { return false; }
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a); //append autocomplete container
for (i = 0; i < arr.length; i++) { //forEach element in array
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) { //if input.upper() = thatArray.substr(len).upper():
b = document.createElement("DIV"); // for that matching element
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>"; //bold matches
b.innerHTML += arr[i].substr(val.length); //remaining non-matches
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>"; //clickDetecting
b.addEventListener("click", function(e) { //if user selects,
inp.value = this.getElementsByTagName("input")[0].value; //autocomplete for input field,
closeAllLists(); //then close suggestions container
});
a.appendChild(b); //add below input field
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) { //down
currentFocus++; //increase currentFocus
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--; //reduce currentFocus
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault(); //prevent return key submitting form
if (currentFocus > -1) {
if (x) x[currentFocus].click(); //autosuggest by click simulation.
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
removeActive(x); //remove all first
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active"); //highlight
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active"); //remove highlight from all
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items"); //remove every child from autosuggest
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function(e) {
closeAllLists(e.target);
});
}