-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsearch.js
More file actions
32 lines (28 loc) · 2.18 KB
/
search.js
File metadata and controls
32 lines (28 loc) · 2.18 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
// This is a function called startSearch that is used to search a list of items for a specific search term.
// Here's a breakdown of what the function does:
// It declares a few variables:
// input is a reference to an element with an ID of "mySearch", which is an input element where the user types in the search term.
// filter is the search term the user has typed in, converted to uppercase.
// ul is a reference to an element with an ID of "search", which is an unordered list containing the items to be searched.
// li is a collection of elements within the ul element, with the tag name "a".
// a is a variable that will be used to reference each element in the li collection.
// i is a loop counter variable.
// The function then starts a loop that will iterate over each element in the li collection.
// On each iteration of the loop, the variable a is assigned the value of the first element within the current li element with the tag name "h1".
// The function checks to see if the text within the element referenced by a (which should be the text of an h1 element) contains the search term (filter). It does this by checking the index returned by the indexOf method called on the element's text (converted to uppercase). If the search term is found, the indexOf method will return a number greater than -1. If the search term is found, the current li element's style.display property is set to an empty string (which will make the element visible). If the search term is not found, the li element's style.display property is set to "none" (which will hide the element). The loop then moves on to the next element in the li collection and repeats the process until all elements have been checked.
// Help generated by ChatGPT
function startSearch() {
var input, filter, ul, li, a, i;
input = document.getElementById("mySearch");
filter = input.value.toUpperCase();
ul = document.getElementById("search");
li = ul.getElementsByTagName("a");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("h1")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}