-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
57 lines (48 loc) · 1.64 KB
/
index.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
window.addEventListener("load", function(){
// Add a keyup event listener to our input element
document.getElementById('name_input').addEventListener("keyup", function(event){hinter(event)});
// create one global XHR object
// so we can abort old requests when a new one is make
window.hinterXHR = new XMLHttpRequest();
});
// Autocomplete for form
function hinter(event) {
var input = event.target;
var huge_list = document.getElementById('huge_list');
// minimum number of characters before we start to generate suggestions
var min_characters = 0;
if (!isNaN(input.value) || input.value.length < min_characters ) {
return;
} else {
window.hinterXHR.abort();
window.hinterXHR.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse( this.responseText );
huge_list.innerHTML = "";
response.forEach(function(item) {
// Create a new <option> element.
var option = document.createElement('option');
option.value = item;
huge_list.appendChild(option);
});
}
};
window.hinterXHR.open("GET", "/query.php?query=" + input.value, true);
window.hinterXHR.send()
}
}
function validateForm(){
// Get the input element
var input = document.getElementById('name_input');
// Get the datalist
var huge_list = document.getElementById('huge_list');
// If we find the input inside our list, we submit the form
for (var element of huge_list.children) {
if(element.value == input.value) {
return true;
}
}
// we send an error message
alert("name input is invalid")
return false;
}