-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter-list.html
More file actions
91 lines (91 loc) · 2.96 KB
/
Copy pathfilter-list.html
File metadata and controls
91 lines (91 loc) · 2.96 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
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"
/>
<title>My Contacts</title>
</head>
<body>
<div class="container">
<h1 class="center-align">My Contacts</h1>
<input id="filter-input" placeholder="Search names" type="text" />
<ul id="names" class="collection width-header">
<li class="collection-header">
<h5>A</h5>
</li>
<li class="collection-item">
<a href="#">Abe</a>
</li>
<li class="collection-item">
<a href="#">Adam</a>
</li>
<li class="collection-item">
<a href="#">Alan</a>
</li>
<li class="collection-item">
<a href="#">Anna</a>
</li>
<li class="collection-header">
<h5>B</h5>
</li>
<li class="collection-item">
<a href="#">Beth</a>
</li>
<li class="collection-item">
<a href="#">Bill</a>
</li>
<li class="collection-item">
<a href="#">Bob</a>
</li>
<li class="collection-item">
<a href="#">Brad</a>
</li>
<li class="collection-header">
<h5>C</h5>
</li>
<li class="collection-item">
<a href="#">Carrie</a>
</li>
<li class="collection-item">
<a href="#">Cathy</a>
</li>
<li class="collection-item">
<a href="#">Courtney</a>
</li>
</ul>
</div>
<script>
// Grab text input for searching
let filterInput = document.getElementById("filter-input");
// On keyup, filter the list of names
filterInput.addEventListener("keyup", filterNames);
function filterNames() {
// Grab the text value inside the search input and transform it to upper case for consistency
let filterValue = document
.getElementById("filter-input")
.value.toUpperCase();
// Grab the unordered list
let ul = document.getElementById("names");
// Grab all of the elements that contain contact names
let li = ul.querySelectorAll("li.collection-item");
// We need to iterate over each of the collection item elements
for (let i = 0; i < li.length; i++) {
// Grab the list item at the current indexs a tag and get the value of the html collection
let a = li[i].getElementsByTagName("a")[0];
// If the text contains the filter value, persist it
if (a.innerHTML.toUpperCase().indexOf(filterValue) > -1) {
li[i].style.display = "";
// If not, we want to make it disappear
} else {
li[i].style.display = "none";
}
}
}
</script>
</body>
</html>