How to Create Filter Search List in JavaScript

How to Create Filter Search List in JavaScript


In this tutorial we will create a How to Create Filter Search List in JavaScript. This tutorial purpose is to enable you to search a list with the filter function. This will cover all the basic function that will search a list. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system that needed to to filter any important data. I will give my best to provide you the easiest way of creating this program Filter Search List. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only list and the search box. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Create Filter Search List in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div class="form-group">
  18. <div class="input-group">
  19. <div class="input-group-addon"><span class="glyphicon glyphicon-search"></span></div>
  20. <input type="text" id="search" placeholder="Search here..." onkeyup="searchResult()" class="form-control"/>
  21. </div>
  22. </div>
  23. <ul class="list-group" id="list"></ul>
  24. </div>
  25. </div>
  26. <script src="script.js"></script>
  27. </body>
  28. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will try to filter the entered result in the list. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let name_list = ['Steve', 'Bruce', 'Tony', 'Rimuru', 'Millim', 'Leon', 'Takamitchy', 'Draken'];
  3. displayList();
  6. function searchResult(){
  7. let search = document.getElementById('search').value.toLowerCase();
  8. let target = document.getElementById("list");
  9. let list = target.getElementsByTagName('li');
  10. for(var i=0; i < list.length; i++){
  11. let text = list[i].innerHTML;
  12. if(text.toLowerCase().indexOf(search) > -1){
  13. list[i].style.display = "";
  14. }else{
  15. list[i].style.display = "none";
  16. }
  17. }
  18. }
  20. function displayList(){
  21. name_list.sort(function(a,b){
  22. if(a < b){
  23. return -1;
  24. }
  26. if(a > b){
  27. return 1;
  28. }
  30. return 0;
  31. });
  33. data = "";
  35. for(var i=0; i < name_list.length; i++){
  36. data += "<li class='list-group-item'><a href='"+name_list[i]+"'>"+name_list[i]+"</a></li>";
  37. }
  39. document.getElementById('list').innerHTML = data;
  40. }

In the code above we just simply first create a variable that will handle the array of the list. Then we create a new method called displayList(), this function will sort out the filtered data that we enteredfrom our displayed list. And last we will create a method called searchResult(). This function will bind the search box and will display a result base on the entered keyword.


The How to Create Filter Search List in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Filter Search List in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment