JavaScript - Simple Search Bar

Submitted by: 
Visitors have accessed this post 116 times.

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

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Search Bar</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-5">
  18. <div class="form-group">
  19. <input type="text" id="search" placeholder="Search here..." onkeyup="search()" class="form-control"/>
  20. </div>
  21. <ul class="list-group" id="list">
  23. <li class='list-group-item'>1. Apple</li>
  24. <li class='list-group-item'>2. Banana</li>
  25. <li class='list-group-item'>3. Cat</li>
  26. <li class='list-group-item'>4. Dog</li>
  27. <li class='list-group-item'>5. Eggplant</li>
  28. <li class='list-group-item'>6. Flame</li>
  29. </ul>
  30. </div>
  31. </div>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.