-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
28 lines (24 loc) · 1.08 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
document.addEventListener('DOMContentLoaded', async () => {
const searchInput = document.getElementById('searchInput');
const searchResults = document.getElementById('searchResults');
const searchResultTemplate = document.getElementById('searchResultTemplate').innerHTML;
searchResults.innerHTML = 'Games are loading...';
try {
const data = await fetch('games.json').then(response => response.json());
const renderSearchResults = (searchTerm = '') => {
searchResults.innerHTML = '';
const filteredResults = data.filter(item => item.title.toLowerCase().includes(searchTerm.toLowerCase()));
filteredResults.forEach(result => {
const renderedTemplate = searchResultTemplate.replace(/{{(.*?)}}/g, (match, key) => result[key.trim()]);
searchResults.innerHTML += renderedTemplate;
});
};
renderSearchResults();
searchInput.addEventListener('input', () => {
const searchTerm = searchInput.value.trim();
renderSearchResults(searchTerm);
});
} catch (error) {
console.error('Error fetching data:', error);
}
});