JavaScript - Simple Dropdown Search

In this tutorial we will create a Simple Dropdown Search using JavaScript. This code will change the native way of HTML select option by applying one of the jQuery plugins. This code use select2 plugin to populate and apply a search engine to HTML select. 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/. And, this is the link for the jQuery plugin that I used https://select2.org/

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 it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4.         <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />             
  5.         <link rel="stylesheet" type="text/css" href="css/select2.css" />               
  6. </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <div class="container-fluid">
  10.                                 <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                         </div>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">JavaScript - Simple Dropdown Search</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-3"></div>
  19.                 <div class="col-md-6">
  20.                         <div class="form-group">
  21.                                 <select id="movie_list"  class="form-controls"></select>
  22.                         </div>
  23.                         <center><button class="btn btn-primary" id="submit">Submit</button></center>
  24.                 </div>
  25.                 </div>
  26.         </div>
  27.  
  28.        
  29. <script src="js/jquery-3.2.1.min.js" type="text/javascript"></script>  
  30. <script src="js/select2.js"></script>
  31. <script src="js/script.js"></script>
  32.  
  33. </body>
  34. </html>

Creating the Script

This code contains the script of the application. This code will change the select option into an advance search engine. 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. $(document).ready(function() {
  2.         var movies = ["", "Captain America: The First Avenger", "Captain Marvel", "Iron Man", "Iron Man 2", "The Incredible Hulk", "Thor", "The Avengers", "Iron Man 3", "Thor: The Dark World", "Captain America: The Winter Soldier", "Guardians of the Galaxy", "Guardians of the Galaxy Vol. 2", "Avengers: Age of Ultron", "Ant-Man", "Captain America: Civil War", "Spider-Man: Homecoming", "Doctor Strange", "Black Panther", "Thor: Ragnarok", "Avengers: Infinity War", "Antmant And The Wasp", "Avengers: Endgame"];
  3.         $("#movie_list").select2({
  4.                 data: movies,
  5.                 placeholder: "Select a movie",
  6.                 sorter: function(data) {
  7.                         return data.sort(function (a, b) {
  8.                                 a = a.text.toLowerCase();
  9.                                 b = b.text.toLowerCase();
  10.                                 if (a > b) {
  11.                                         return 1;
  12.                                 } else if (a < b) {
  13.                                         return -1;
  14.                                 }
  15.                                         return 0;
  16.                         });
  17.                 }
  18.                  
  19.         });
  20.        
  21.         $("#submit").on('click', function(){
  22.                 var movie = $('#movie_list').val();
  23.                
  24.                 if(movie === ""){
  25.                         alert("Please select something first");
  26.                 }else{
  27.                         alert("You choose: "+movie);
  28.                 }
  29.         });
  30. });
There you have it we successfully created a Simple Dropdown 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