Auto Search Using JavaScript

In this code we will tackle about Auto Search using JavaScript. The program will enable to automatically search a row data. You are free to modify, and use this code. To learn more about this, just follow the steps below.

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">Auto Search Using JavaScript</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="autoSearch()" class="form-control"/>
  22.                                 </div>
  23.                         </div>
  24.                 </div>
  25.                
  26.                 <table class="table table-bordered">
  27.                         <thead class="alert-info">
  28.                                 <tr>
  29.                                         <th>Member List</th>
  30.                                 </tr>
  31.                         </thead>
  32.                         <tbody id="result"></tbody>
  33.                 </table>
  34.                 <ul class="list-group" id="list"></ul>
  35.                
  36.         </div>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the Script

This code contains the script of the application. The code will automatically search a specific keyword. 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', 'Jin Mori', 'Luke Cage'];
  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 autoSearch(){
  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 Auto 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