-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
85 lines (74 loc) · 2.48 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Item Search</title>
<style>
#search-container {
margin-top: 20px;
}
#item-list {
list-style-type: none;
padding: 0;
}
.item {
border: 1px solid #ccc;
padding: 10px;
margin: 5px;
}
</style>
</head>
<body>
<h1>Item Search</h1>
<div id="search-container">
<input type="text" id="search-input" placeholder="Search items...">
</div>
<ul id="item-list"></ul>
<script>
const itemList = document.getElementById('item-list');
const searchInput = document.getElementById('search-input');
let searchTimeout;
// Function to fetch items from the API
function fetchItems() {
fetch('https://jsonplaceholder.typicode.com/photos')
.then(response => response.json())
.then(data => {
items = data;
displayItems(items);
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
// Function to display items in the UI
function displayItems(items) {
itemList.innerHTML = ''; // Clear the previous items
items.forEach(item => {
const listItem = document.createElement('li');
listItem.className = 'item';
listItem.innerHTML = `<strong>${item.title}</strong> - <a href="${item.url}" target="_blank">View Image</a>`;
itemList.appendChild(listItem);
});
}
// Function to filter items based on search input
function filterItems(query) {
const filteredItems = items.filter(item =>
item.title.toLowerCase().includes(query.toLowerCase())
);
displayItems(filteredItems);
}
let items = []; // Array to store all items
// Event listener for search input (automatic search with delay)
searchInput.addEventListener('input', (e) => {
const query = e.target.value;
// Clear any previous search timeout
clearTimeout(searchTimeout);
// Set a new search timeout
searchTimeout = setTimeout(() => {
filterItems(query);
}, 300); // 300 milliseconds delay
});
// Initial load of items
fetchItems();
</script>
</body>
</html>