JavaScript - Simple Searchable List

Submitted by: 
Visitors have accessed this post 315 times.

In this tutorial we will create a Simple Searchable List using JavaScript. The code can filter a specific data on the list base on the key word entered by the user. The code itself use indexOf() to filter the array index position of the list, and return an arrange data list in ascending format. This is a user-friendly program feel free to modify it.

We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

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. ul li:hover{
  7. cursor:pointer;
  8. background-color:#eee;
  9. }
  10. </style>
  11. </head>
  12. <nav class="navbar navbar-default">
  13. <div class="container-fluid">
  14. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  15. " rel="nofollow">">Sourcecodester</a>
  16. </a> </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">Javascript - Simple Searchable List</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <div class="col-md-2"></div>
  23. <div class="col-md-8">
  24. <div class="form-inline">
  25. <input type="text" id="search" placeholder="Search here..." class="form-control"/>
  26. <button class="btn btn-primary" onclick="search();"><span class="glyphicon glyphicon-search"></span> Search</button>
  27. </div>
  28. <br />
  29. <div class="alert alert-success">My Movie List</div>
  30. <ul class="list-group" id="list"></ul>
  31. </div>
  32. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will filter the list when the user submit the keyword in the form. 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 directory

  1. var movie_list = ['Capptain America', 'Ant-Man', 'Doctor Strange', 'Thor: Ragnarok', 'Deadpool', 'Black Panther', 'Iron Man 3', 'Captain Marvel'];
  3. function select(index){
  4. alert("You Select: "+movie_list[index]);
  5. }
  7. function search(){
  8. var search = document.getElementById('search').value.toLowerCase();
  9. var target = document.getElementById("list");
  10. var list = target.getElementsByTagName('li');
  11. for(var i=0; i < list.length; i++){
  12. var text = list[i].innerHTML;
  13. if(text.toLowerCase().indexOf(search) > -1){
  14. list[i].style.display = "";
  15. }else{
  16. list[i].style.display = "none";
  17. }
  18. }
  19. }
  21. function displayList(){
  22. movie_list.sort(function(a,b){
  23. if(a < b){
  24. return -1;
  25. }
  27. if(a > b){
  28. return 1;
  29. }
  31. return 0;
  32. });
  34. data = "";
  36. for(var i=0; i < movie_list.length; i++){
  37. data += "<li class='list-group-item' onclick='select("+i+");'>"+movie_list[i]+"</li>";
  38. }
  40. document.getElementById('list').innerHTML = data;
  41. }
  43. displayList();

There you have it we successfully created a Simple Searchable List 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.