-
Notifications
You must be signed in to change notification settings - Fork 0
/
accordion.js
60 lines (50 loc) · 2.15 KB
/
accordion.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
function solution() {
let baseUrl = 'http://localhost:3030/jsonstore/advanced/articles/';
let mainSection = document.getElementById('main');
let extraDiv;
let parArticle;
fetch(baseUrl + 'list')
.then((res) => res.json())
.then((data) => {
for (each of data) {
let accordionDiv = document.createElement('div');
accordionDiv.setAttribute('class', 'accordion');
let headDiv = document.createElement('div');
headDiv.setAttribute('class', 'head');
extraDiv = document.createElement('div');
extraDiv.setAttribute('class', 'extra');
let spanTitle = document.createElement('span');
spanTitle.textContent = each.title;
let buttonId = document.createElement('button');
buttonId.setAttribute('class', 'button');
buttonId.textContent = 'More';
buttonId.setAttribute('id', each._id);
mainSection.appendChild(accordionDiv);
accordionDiv.appendChild(headDiv);
accordionDiv.appendChild(extraDiv);
headDiv.appendChild(spanTitle);
headDiv.appendChild(buttonId);
parArticle = document.createElement('p');
extraDiv.appendChild(parArticle);
buttonId.addEventListener('click', showHide);
}
});
function showHide(e) {
fetch(baseUrl + 'details/' + e.target.id)
.then((res) => res.json())
.then((data) => {
let a = e.target.parentNode.parentNode;
let b = a.querySelector('[class="extra"]');
if (e.target.textContent == 'More') {
e.target.textContent = 'Less';
parArticle.textContent = data.content;
b.appendChild(parArticle);
b.style.display = 'block';
} else if (e.target.textContent == 'Less') {
e.target.textContent = 'More';
b.style.display = 'none';
}
});
}
}
solution();