Skip to main content

JavaScript - Simple Search Bar


In this tutorial we will create a Simple Search Bar using JavaScript. This code will search a specific data every time the user enter some characters in the textbox. The code use onkeyup() function to call a specific function that automatically filter the data when the keyword is entered, by using for() loop and adding a if statement indexOf() you can search the index position of an item from 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 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 Bar</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-5">
  17. <div class="form-group">
  18. <input type="text" id="search" placeholder="Search here..." onkeyup="search()" class="form-control"/>
  19. </div>
  20. <ul class="list-group" id="list">
  21.  
  22. <li class='list-group-item'>1. Apple</li>
  23. <li class='list-group-item'>2. Banana</li>
  24. <li class='list-group-item'>3. Cat</li>
  25. <li class='list-group-item'>4. Dog</li>
  26. <li class='list-group-item'>5. Eggplant</li>
  27. <li class='list-group-item'>6. Flame</li>
  28. </ul>
  29. </div>
  30. </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will display the list of of data based on a given keyword. 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. function search(){
  2. var search = document.getElementById('search').value.toLowerCase();
  3. var target = document.getElementById("list");
  4. var list = target.getElementsByTagName('li');
  5. for(var i=0; i < list.length; i++){
  6. var text = list[i].innerHTML;
  7. if(text.toLowerCase().indexOf(search) > -1){
  8. list[i].style.display = "";
  9. }else{
  10. list[i].style.display = "none";
  11. }
  12. }
  13. }

There you have it we successfully created a Simple Search Bar 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.