-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (66 loc) · 2.85 KB
/
script.js
File metadata and controls
81 lines (66 loc) · 2.85 KB
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
document.addEventListener('DOMContentLoaded', () => {
const faqContainer = document.getElementById('faqContainer');
const searchInput = document.getElementById('searchInput');
// Fetch the JSON data for the questions
fetch('questions.json')
.then(response => response.json())
.then(data => {
renderFAQ(data);
})
.catch(error => {
console.error('Error loading FAQ data:', error);
faqContainer.innerHTML = '<p style="text-align:center; color: #ef4444;">Error loading data. Please ensure you are running this on a web server (not directly from file).</p>';
});
// Render HTML from questions.json
function renderFAQ(categories) {
faqContainer.innerHTML = ''; // clear loading state
categories.forEach(cat => {
const catDiv = document.createElement('div');
catDiv.className = 'category';
const catTitle = document.createElement('h2');
catTitle.className = 'category-title';
catTitle.textContent = cat.category;
catDiv.appendChild(catTitle);
cat.items.forEach(item => {
const details = document.createElement('details');
details.className = 'faq-item';
const summary = document.createElement('summary');
summary.textContent = item.question;
const answerDiv = document.createElement('div');
answerDiv.className = 'answer';
answerDiv.innerHTML = `<p>${item.answer}</p>`;
details.appendChild(summary);
details.appendChild(answerDiv);
catDiv.appendChild(details);
});
faqContainer.appendChild(catDiv);
});
}
// search functionality
searchInput.addEventListener('keyup', function(e) {
const term = e.target.value.toLowerCase();
const categories = document.querySelectorAll('.category');
categories.forEach(category => {
const questions = category.querySelectorAll('.faq-item');
let hasVisibleQuestions = false;
questions.forEach(question => {
const text = question.innerText.toLowerCase();
if(text.includes(term)) {
question.classList.remove('hidden');
if(term.length > 2) {
question.setAttribute('open', true);
}
hasVisibleQuestions = true;
} else {
question.classList.add('hidden');
question.removeAttribute('open');
}
});
if(hasVisibleQuestions) {
category.classList.remove('hidden');
} else {
category.classList.add('hidden');
}
});
});
});