JavaScript - Real Time Search

In this tutorial we will create a Real Time Search using JavaScript. This code will search a specific data every time the user enter some characters in the textbox. The code use onclick() function to call a specific function that automatically filter the data when entered, by using a for() loop and adding a if statement with an argument indexOf() in order to filter the index position of an array. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

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 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="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 - Real Time Search</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-group">
  18.                                 <label>Enter a name</label>
  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="liveSearch()" class="form-control"/>
  22.                                 </div>
  23.                         </div>
  24.                 </div>
  25.                 <div class="col-md-8">
  26.                         <table class="table table-bordered">
  27.                                 <thead class="alert-info">
  28.                                         <tr>
  29.                                                 <th>Full Name</th>
  30.                                         </tr>
  31.                                 </thead>
  32.                                 <tbody id="result"></tbody>
  33.                         </table>
  34.                         <ul class="list-group" id="list"></ul>
  35.                 </div>
  36.         </div>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the Script

This code contains the script of the application. This code will search a data when entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var members = ['Tony Stark', 'Steve Rogers', 'John Smith', 'Claire Temple', 'Grace Toha', 'James Yhup', 'Isko Morren', 'Natalya Flash'];
  2.        
  3. displayMember();
  4.  
  5. function displayMember(){
  6.         members.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 < members.length; i++){
  21.                 data += "<tr><td>"+members[i]+"</td></tr>";
  22.         }
  23.        
  24.         document.getElementById('result').innerHTML = data;
  25. }
  26.  
  27. function liveSearch(){
  28.         var search=document.getElementById('search').value.toLowerCase();
  29.         var parent=document.getElementById("result");
  30.         var children=parent.getElementsByTagName('tr');
  31.        
  32.         for(var i=0; i<children.length; i++){
  33.                 var keyword=children[i].innerText;
  34.                 if(keyword.toLowerCase().indexOf(search)>-1){
  35.                         children[i].style.display="";
  36.                 }else{
  37.                         children[i].style.display = "none";
  38.                 }
  39.         }
  40.        
  41. }
There you have it we successfully created a Real Time Search 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