-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
34 lines (28 loc) · 963 Bytes
/
search.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
const items = [
'JavaScript programming',
'HTML and CSS basics',
'Frontend development',
'Web design principles',
'Dynamic content handling'
];
const searchInput = document.getElementById('searchInput');
const itemList = document.getElementById('itemList');
function highlightText(text, query) {
const regex = new RegExp(`(${query})`, 'gi');
return text.replace(regex, '<span class="highlight">$1</span>');
}
function filterItems() {
const query = searchInput.value.toLowerCase();
itemList.innerHTML = '';
items.forEach(item => {
if (item.toLowerCase().includes(query)) {
const highlightedText = highlightText(item, query);
const listItem = document.createElement('li');
listItem.innerHTML = highlightedText;
itemList.appendChild(listItem);
}
});
}
searchInput.addEventListener('input', filterItems);
// Initialize list with all items
filterItems();