-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
85 lines (60 loc) · 1.87 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// imports
import Autocomplete from './Autocomplete';
import usStates from './us-states';
//import keys from './keys.json';
import './main.css';
// US States
let data = usStates.map(state => ({
text: state.name,
value: state.abbreviation
}));
new Autocomplete(document.getElementById('state'), {
data,
onSelect: (stateCode) => {
console.log('selected state:', stateCode);
},
});
new Autocomplete(document.getElementById('gh-user'),
{
onSelect: (ghUserId) => {
console.log('selected github user id:', ghUserId);
},
});
// ====== //
// starting to work on jQuery
$(document).on("click", "ul.results", function(event){
event.preventDefault();
$(this).find(">:first-child").addClass("jqhover");
let current = $(this).find(">:first-child");
current.addClass("jqhover");
// console.log( $(this).attr("data-number"));
// console.log( $(this).text());
});
// add event listener to whole window
document.addEventListener("keydown", function(e){
// capture value of keycode when pressed
// only keydown allows arrow key capture
var key = e.which;
// console.log(key)
// if ($("li").hasClass("jqhover")){
// console.log("true")
// set current value to <li> with jqhover class
let current = $("li.jqhover");
// set currentNum to the value of data-number attribute (index)
let currentNum = current.attr("data-number");
//console.log(currentNum);
//Capture down arrow
if (key == 40){
// set next to sibling element
let next = current.next();
// set nextNum to data-number of sibling element
let nextNum = next.attr("data-number");
// if nextNum is defined and its data-number value is greater than currentNum (bc ascending order that means it exists)
if (nextNum && nextNum > currentNum){
// everything shifts up
current.removeClass("jqhover");
next.addClass("jqhover");
}
};
//Need to capture up arrow
});