-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
123 lines (96 loc) · 4.24 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
window.onload = function() {
// Mobile Nav
const menu_btn = document.querySelector('.hamburger');
const mobile_menu = document.querySelector('.mobile-nav');
menu_btn.addEventListener('click', function () {
menu_btn.classList.toggle('is-active');
mobile_menu.classList.toggle('is-active');
});
// Displaying Excuses from excuser API
// Display Random Excuse Widescreen
const randomBtn = document.querySelector('.btn-random-widescreen');
const box = document.querySelector(".content");
const newH = document.createElement('h3');
const labelBox = document.querySelector('.label');
const newLabel = document.createElement('h4');
newLabel.classList.add('label-text');
randomBtn.addEventListener('click', () => {
const h = document.querySelector('h3');
const label = document.querySelector('h4');
label.parentElement.removeChild(label);
newLabel.innerText = randomBtn.innerText;
labelBox.prepend(newLabel);
let url = "https://excuser.herokuapp.com/v1/excuse";
fetch(url)
.then(response => response.json())
.then(response => {
let text = response[0].excuse;
newH.innerText = `"${text}"`;
h.parentElement.removeChild(h);
box.appendChild(newH);
})
});
// Display Random Excuse by Category Widescreen
const cat_btns_wide = document.querySelectorAll('.btn-cat-widescreen');
cat_btns_wide.forEach(catBtn => catBtn.addEventListener('click', () => {
const h = document.querySelector('h3');
const label = document.querySelector('h4');
label.parentElement.removeChild(label);
newLabel.innerText = catBtn.innerText;
labelBox.prepend(newLabel);
const categoryName = catBtn.innerText.toLowerCase();
let catUrl = `https://excuser.herokuapp.com/v1/excuse/${ categoryName }`;
fetch(catUrl)
.then(response => response.json())
.then(response => {
let text = response[0].excuse;
newH.innerText = `"${text}"`;
h.parentElement.removeChild(h);
box.appendChild(newH);
})
}))
// Display Random Excuse Mobile
const randomBtnMobile = document.querySelector('.btn-random-mobile');
randomBtnMobile.addEventListener('click', () => {
if (mobile_menu.classList.contains('is-active')) {
mobile_menu.classList.remove('is-active');
menu_btn.classList.remove('is-active');
}
const h = document.querySelector('h3');
const label = document.querySelector('h4');
label.parentElement.removeChild(label);
newLabel.innerText = randomBtn.innerText;
labelBox.prepend(newLabel);
let url = "https://excuser.herokuapp.com/v1/excuse";
fetch(url)
.then(response => response.json())
.then(response => {
let text = response[0].excuse;
newH.innerText = `"${text}"`;
h.parentElement.removeChild(h);
box.appendChild(newH);
})
});
const cat_btns_mobile = document.querySelectorAll('.btn-cat-mobile');
cat_btns_mobile.forEach(button => button.addEventListener('click', () => {
if (mobile_menu.classList.contains('is-active')) {
mobile_menu.classList.remove('is-active');
menu_btn.classList.remove('is-active');
}
const h = document.querySelector('h3');
const label = document.querySelector('h4');
label.parentElement.removeChild(label);
newLabel.innerText = button.innerText;
labelBox.prepend(newLabel);
const categoryName = button.innerText.toLowerCase();
let catUrl = `https://excuser.herokuapp.com/v1/excuse/${ categoryName }`;
fetch(catUrl)
.then(response => response.json())
.then(response => {
let text = response[0].excuse;
newH.innerText = `"${text}"`;
h.parentElement.removeChild(h);
box.appendChild(newH);
})
}));
};