JavaScript - Simple Search Filter

In this tutorial we will create a Simple Search Filter using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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="https://sourcecodester.com">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">Javascript - Simple Search Filter</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <div class="form-group">
  19.                                 <div class="input-group">
  20.                                         <div class="input-group-addon"><span class="glyphicon glyphicon-search"></span></div>
  21.                                         <input type="text" id="search" placeholder="Search here..." onkeyup="searchResult()" class="form-control"/>
  22.                                 </div>
  23.                         </div>
  24.                         <ul class="list-group" id="list"></ul>
  25.                 </div>
  26.         </div>
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>

Creating the Script

This code contains the script of the application. This code will display the list of names based on the keyword that have been inputted by the user. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var name_list = ['Malik', 'Orlando', 'Gautvin', 'Olegario', 'Maurus', 'Priya', 'Borko', 'Piruz'];
  2.        
  3. displayList();
  4.  
  5. function displayList(){
  6.         name_list.sort(function(a,b){
  7.                 if(a < b){
  8.                         return -1;
  9.                 }
  10.  
  11.                 if(a > b){
  12.                         return 1;
  13.                 }
  14.  
  15.                 return 0;
  16.         });
  17.        
  18.         data = "";
  19.        
  20.         for(var i=0; i < name_list.length; i++){
  21.                 data += "<li class='list-group-item'>"+name_list[i]+"</li>";
  22.         }
  23.        
  24.         document.getElementById('list').innerHTML = data;
  25. }
  26.  
  27. function searchResult(){
  28.         var search = document.getElementById('search').value.toLowerCase();
  29.         var target = document.getElementById("list");
  30.         var list = target.getElementsByTagName('li');
  31.         for(var i=0; i < list.length; i++){
  32.                 var text = list[i].innerHTML;
  33.                 if(text.toLowerCase().indexOf(search) > -1){
  34.                         list[i].style.display = "";
  35.                 }else{
  36.                         list[i].style.display = "none";
  37.                 }
  38.         }
  39. }
There you have it we successfully created a Simple Search Filter using 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!

Add new comment