How to make a filter using javascript
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Filter</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<ul id="myUL">
<li><a href="#">Anton</a></li>
<li><a href="#">Andrey</a></li>
<li><a href="#">Bogdan</a></li>
<li><a href="#">Bill</a></li>
<li><a href="#">Boris</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
</ul>
<script src="script.js"></script>
</body>
</html>
CSS
#myInput{
width: 100%;
font-size: 16px;
padding: 12px 20px 12 px 5 px;
border: 1 px solid #ddd;
margin-bottom: 12px;
}
#myUL{
list-style: none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
background-color: f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block;
}
#myUL li a.header{
background-color: #e2e2e2;
cursor: default;
}
#myUL li a:hover:not(.header){
background-color: #eee;
}
JS
function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Filter</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<ul id="myUL">
<li><a href="#">Anton</a></li>
<li><a href="#">Andrey</a></li>
<li><a href="#">Bogdan</a></li>
<li><a href="#">Bill</a></li>
<li><a href="#">Boris</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
</ul>
<script src="script.js"></script>
</body>
</html>
CSS
#myInput{
width: 100%;
font-size: 16px;
padding: 12px 20px 12 px 5 px;
border: 1 px solid #ddd;
margin-bottom: 12px;
}
#myUL{
list-style: none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
background-color: f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block;
}
#myUL li a.header{
background-color: #e2e2e2;
cursor: default;
}
#myUL li a:hover:not(.header){
background-color: #eee;
}
JS
function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
Комментарии
Отправить комментарий