JavaScript - Simple Multiple Dropdown Selection

In this tutorial we will create a Simple Multiple Dropdown Selection using JavaScript. This code will change the HTML select option into an advance dropdown selection. The code itself use chosen to add an attributes that allow you to have a full control of your html select options, and trigger several events on the original select field to invoke a specific behavior. This is a free program feel free to modify and apply it in your system. We will be using JavaScript as a server-side scripting language that interpret a program to extend a whole new level of HTML experience. A scripting language that use in a different ways, and provide a whole level of experience when visiting a 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://jquery.com/ Lastly, this is the link for the chosen plugin that I used https://harvesthq.github.io/chosen/

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/chosen.css" />
  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 Multiple Dropdown Selection</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="form-group">
  17.                         <div class="col-md-3"></div>
  18.                         <div class="col-md-6">
  19.                                 <select data-placeholder="Select an option" id="data" class="chzn-select form-control" multiple="multiple">
  20.                                         <option value="Python">Python</option> 
  21.                                         <option value="PHP">PHP</option>
  22.                                         <option value="JavaScript">JavaScript</option> 
  23.                                         <option value="C#">C#</option> 
  24.                                         <option value="C++">C++</option>
  25.                                 </select>
  26.                                 <br /><br />
  27.                                 <center><button type="button" class="btn btn-primary" id="submit">Submit</button></center>
  28.                         </div>
  29.                 </div>
  30.         </div>  
  31. <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
  32. <script type="text/javascript" src="js/chosen.jquery.js"></script>
  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 turn the native HTML select into an advance dropdown selection. 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. $(".chzn-select").chosen();
  2. $(".chzn-select-deselect").chosen({
  3.         allow_single_deselect:true
  4. });
  5.  
  6. $('#submit').on('click', function(){
  7.         alert("You select: "+$('#data').val());
  8. });
There you have it we successfully created a Simple Multiple Dropdown Selection 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