Created
January 5, 2018 23:50
-
-
Save ldco2016/0cee9b6b8eb6f17d69e1df668b30a4cc to your computer and use it in GitHub Desktop.
Filterable contact list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<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/0.100.2/css/materialize.css"> | |
<title>Thinkful Contacts</title> | |
</head> | |
<body> | |
<div class="container"> | |
<h1 class="center-align"> | |
Thinkful Contacts | |
</h1> | |
<input type="text" id="filterInput" placeholder="Search names..."> | |
<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 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> | |
</li> | |
</ul> | |
</div> | |
<script type="text/javascript"> | |
// Get Input Element | |
let filterInput = document.getElementById('filterInput'); | |
// Add Event Listener | |
filterInput.addEventListener('keyup', filterNames); | |
function filterNames(){ | |
// Get Value of Input | |
let filterValue = document.getElementById('filterInput').value.toUpperCase(); | |
// Get names ul | |
let ul = document.getElementById('names'); | |
// Get lis from ul | |
let li = ul.querySelectorAll('li.collection-item'); // very handy function that can replace getElementById() | |
// Loop through collection-item lis | |
for (var i = 0; i < li.length; i++) { | |
let a = li[i].getElementsByTagName('a')[0]; | |
// If matched | |
if (a.innerHTML.toUpperCase().indexOf(filterValue) > -1) { | |
li[i].style.display = ''; | |
} else { | |
li[i].style.display = 'none'; | |
} | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment