-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (64 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import './style.css';
// GET DATA FROM API
const menuValue = [
{
value: 'CSS Ninja',
},
{
value: 'Master of JavaScript',
},
{
value: 'React Professional',
},
{
value: 'Angular Developer',
},
{
value: 'C#/.NET Engineer',
},
];
const ddMenu = document.querySelector('.selector');
const caret = document.querySelector('.caret');
let userSelection = document.getElementById('user-selection');
// MENU TOGGLE LISTENER
ddMenu.addEventListener('click', ddClickHandler);
// DROPDOWN MENU CLICK HANDLER
function ddClickHandler() {
const children = Array.from(ddMenu.parentElement.children);
if (children.length === 1) {
const menu = document.createElement('div');
menu.classList.add('menu-values');
for (var i = 0; i < menuValue.length; i++) {
const item = document.createElement('div');
item.innerText = menuValue[i].value;
item.classList.add('item');
menu.appendChild(item);
setMenuListeners(item);
}
ddMenu.parentElement.append(menu);
} else {
const items = Array.from(children[1].children);
removeMenuListeners(items);
closeMenu(children);
}
caret.classList.toggle('rotate-caret');
}
// SET MENU LISTENERS
function setMenuListeners(elem) {
elem.addEventListener('click', menuItemHandler);
}
// REMOVE MENU LISTENERS
function removeMenuListeners(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i].removeEventListener('click', menuItemHandler);
}
}
// DROPDOWN MENU HANDLER
function menuItemHandler(e) {
userSelection.innerText = e.target.innerText;
ddClickHandler();
}
// CLOSE MENU/ REMOVE FROM DOM
function closeMenu(arr) {
arr[1].remove();
}