-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
29 lines (24 loc) · 1.03 KB
/
script.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
let buttons = Array.from(document.getElementsByClassName("open-close-answer"));
let answers = Array.from(document.getElementsByClassName("answer"));
if (buttons.length !== answers.length){
throw new Error(`not enough buttons: ${buttons.length} buttons ${answers.length} answers`);
}
for (let i=0; i < buttons.length; i++){
let currentButton = buttons[i];
let currentAnswer = answers[i];
let plusIcon = currentButton.children[0];
let minusIcon = currentButton.children[1];
currentButton.addEventListener('click', e=>{
plusIcon.style.display === 'none' ? closeAnswer(plusIcon, minusIcon, currentAnswer) : openAnswer(plusIcon, minusIcon, currentAnswer);
});
}
function openAnswer(plusIcon, minusIcon, currentAnswer){
plusIcon.style.display = 'none';
minusIcon.style.display = 'block';
currentAnswer.style.display = 'block';
}
function closeAnswer(plusIcon, minusIcon, currentAnswer){
plusIcon.style.display = 'block';
minusIcon.style.display = 'none';
currentAnswer.style.display = 'none';
}