Skip to content

Instantly share code, notes, and snippets.

@DpsRager
Created December 22, 2017 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DpsRager/cf12f5e4eca3cb2745f926fde70fbcf2 to your computer and use it in GitHub Desktop.
Save DpsRager/cf12f5e4eca3cb2745f926fde70fbcf2 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewpoint" 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/0.100.2/css/materialize.css">
<title>My Contact</title>
</head>
<body>
<div class="container">
<h1 class="center-align">
My Contacts
</h1>
<input type="text" id="filterInput" placeholder="Search names...">
<ul id="names" class="collection with-header">
<li class="collection-header">
<h5>A</h5>
</li>
<li class="collection-item">
<a href="#">Abe</a>
</li>
<li class="collection-item">
<a href="#">Alan</a>
</li>
<li class="collection-item">
<a href="#">Adam</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>
// GET INPUT ELEMENT
function filterNames() {
//GET VALUE OF INPUT
let filterValue =
document.getElementById('filterInput').value.toUpperCase();
// Get names ul
let ul = document.getElementById('names');
// GET li from Ul
let li = ul.querySelectorAll('li.collection-item');
// LOOP THROUGH collection-item li
for (let i = 0; i < li.length; i++) {
let a = li.[i].getElementByTagName('a')[0];
// IF Matched
if (a.innerHTML.toUpperCase().indexOf(filterValue) > -1) {
li[i].style.display = '';
} else {
li[i].style.display = 'none';
}
}
}
let filterInput = document.getElementById('filterInput');
// ADD EVENT LISTENER
filterInput.addEventListener('keyup', filterNames);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment