-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (41 loc) · 1.39 KB
/
index.js
File metadata and controls
55 lines (41 loc) · 1.39 KB
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
window.onload = function () {
var arr = [
"Max",
"Manu",
"Agra",
"London",
"Cats",
"Dogs",
"Dog fight",
"Dog fighting Cats",
];
document.querySelector('#queryInput').onkeyup = function (i) {
var querySuggestions = [];
if (this.innerHTML != "") {
document.querySelector('.placeholder').style.visibility = "hidden";
var input = this.innerHTML;
var txt = document.createElement("textarea");
txt.innerHTML = input;
input = txt.value;
arr.map((e) => {
var substr = e.substring(0, input.length).toLowerCase();
if (substr.trim() == input.toLowerCase().trim()) {
querySuggestions.push(e);
var suggestion = querySuggestions.reduce((a, b) => a.length <= b.length ? a : b);
console.log(suggestion)
document.querySelector('#suggestedString').innerHTML = suggestion.substring(input.length, suggestion.length);
if (i.keyCode == 39) {
document.querySelector('#queryInput').innerHTML = suggestion;
document.querySelector('#suggestedString').innerHTML = "";
document.querySelector('#queryInput').focus();
document.execCommand('selectAll', false, null);
document.getSelection().collapseToEnd();
}
}
});
} else {
document.querySelector('.placeholder').style.visibility = "visible";
document.querySelector('#suggestedString').innerHTML = "";
}
}
}