I have a list of users as well:
JavaScript
x
9
1
<ul>
2
<li class="thumb selectable arrow light" style="margin-bottom:-5px;"
3
data-image="http://cdn.tapquo.com/lungo/icon-144.png">
4
<strong class="name">Peter <font data-count="0" style="position:relative;top:-2px;"> </font></strong>
5
<small class="description">Hi!</small>
6
</li>
7
8
</ul>
9
what I want is a text input each time you write a letter to display only users that start with that letter or that they might have the name. As I can do? It is with jquery but not as …
Advertisement
Answer
Here is a input
that filters a <ul>
based on the value in pure JavaScript. It works by handling the onkeyup
and then getting the <li>
s and comparing their inner element .name
with the filter text.
JavaScript
1
13
13
1
var input = document.getElementById('input');
2
input.onkeyup = function () {
3
var filter = input.value.toUpperCase();
4
var lis = document.getElementsByTagName('li');
5
for (var i = 0; i < lis.length; i++) {
6
var name = lis[i].getElementsByClassName('name')[0].innerHTML;
7
if (name.toUpperCase().indexOf(filter) == 0)
8
lis[i].style.display = 'list-item';
9
else
10
lis[i].style.display = 'none';
11
}
12
}
13