JavaScript - Simple Searchable List

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 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. 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="https://sourcecodester.com">Sourcecodester</a>
  15. </div>
  16. </nav>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6 well">
  19. <h3 class="text-primary">Javascript - Simple Searchable List</h3>
  20. <hr style="border-top:1px dotted #ccc;"/>
  21. <div class="col-md-2"></div>
  22. <div class="col-md-8">
  23. <div class="form-inline">
  24. <input type="text" id="search" placeholder="Search here..." class="form-control"/>
  25. <button class="btn btn-primary" onclick="search();"><span class="glyphicon glyphicon-search"></span> Search</button>
  26. </div>
  27. <br />
  28. <div class="alert alert-success">My Movie List</div>
  29. <ul class="list-group" id="list"></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 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'];
  2.  
  3. function select(index){
  4. alert("You Select: "+movie_list[index]);
  5. }
  6.  
  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. }
  20.  
  21. function displayList(){
  22. movie_list.sort(function(a,b){
  23. if(a < b){
  24. return -1;
  25. }
  26.  
  27. if(a > b){
  28. return 1;
  29. }
  30.  
  31. return 0;
  32. });
  33.  
  34. data = "";
  35.  
  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. }
  39.  
  40. document.getElementById('list').innerHTML = data;
  41. }
  42.  
  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