Skip to content

Instantly share code, notes, and snippets.

@DpsRager
Created December 19, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DpsRager/b9a8a256b681006270a50f1cfd62d325 to your computer and use it in GitHub Desktop.
Save DpsRager/b9a8a256b681006270a50f1cfd62d325 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
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 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';
}
}
}
</script>
</body>
</html>
@rjoydip-zz
Copy link

Please use let a = li[i].children[0]; not let a = li.[i].getElementByTagName('a')[0];

@dirceu-jr
Copy link

dirceu-jr commented Dec 19, 2017

There is no getElementByTagName function in browsers JS. The correct is getElementsByTagName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment