How to Change List Value Base on Dropdown in JavaScript

How to Change List Value Base on Dropdown in JavaScript

Introduction

In this tutorial we will create a How to Change List Value Base on Dropdown in JavaScript. This tutorial purpose is to teach you how to dynamically change your list value base on the dropdown option. This will thoroughly show you the simple way changing your list . I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that can dynamically update your html list data. I will give my best to provide you the easiest way of creating this program Change List Data Base on Dropdown option. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html list and dropdown buttons. To create this simply copy and write it into your text editor, then save it 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="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">How to Change List Value Base on Dropdown</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-inline">
  18.                                 <h3>Anime Title</h3>
  19.                                 <select onchange="dropdownList(this.value);" class="form-control">
  20.                                         <option value="">Select an option</option>
  21.                                         <option value="dragonball">Dragon Ball</option>
  22.                                         <option value="naruto">Naruto</option>
  23.                                         <option value="onepiece">One Piece</option>
  24.                                         <option value="bleach">Bleach</option>
  25.                                 </select>
  26.                         </div>
  27.                 </div>
  28.                 <div class="col-md-8">
  29.                         <div id="result"></div>
  30.                 </div>
  31.         </div>
  32. <script src="script.js"></script>      
  33. </body>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to dynamically change your html list. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function dropdownList(item){
  2.         switch(item){
  3.                 case "dragonball":
  4.                         var member=["Goku", "Vegeta", "Krillin", "Gohan", "Trunks", "Piccolo"];
  5.                         var html="";
  6.                        
  7.                         html+="<h4>Dragon Ball Character:</h4><br />";
  8.                        
  9.                         html+="<ul>";
  10.                        
  11.                         for(var i=0; i<member.length; i++){
  12.                                 html+="<li>"+member[i]+"</li>";
  13.                         }
  14.                        
  15.                         html+="</ul>";
  16.                        
  17.                         document.getElementById("result").innerHTML=html;
  18.                        
  19.                        
  20.                 break;
  21.                
  22.                 case "naruto":
  23.                         var member=["Naruto", "Sakura", "Sasuke", "Kakashi", "Jiraiya"];
  24.                         var html="";
  25.                        
  26.                         html+="<h4>Naruto Character:</h4><br />";
  27.                        
  28.                         html+="<ul>";
  29.                        
  30.                         for(var i=0; i<member.length; i++){
  31.                                 html+="<li>"+member[i]+"</li>";
  32.                         }
  33.                        
  34.                         html+="</ul>";
  35.                        
  36.                         document.getElementById("result").innerHTML=html;
  37.                 break;
  38.                
  39.                 case "onepiece":
  40.                         var member=["Luffy", "Sanji", "Zoro", "Nami", "Chopper", "Usopp", "Robin", "Jimbe", "Brook", "Frank"];
  41.                         var html="";
  42.                        
  43.                         html+="<h4>One Piece Character:</h4><br />";
  44.                        
  45.                         html+="<ul>";
  46.                        
  47.                         for(var i=0; i<member.length; i++){
  48.                                 html+="<li>"+member[i]+"</li>";
  49.                         }
  50.                        
  51.                         html+="</ul>";
  52.                        
  53.                         document.getElementById("result").innerHTML=html;
  54.                 break;
  55.                
  56.                 case "bleach":
  57.                         var member=["Ichigo", "Rukia", "Inoue", "Sado", "Yoruichi", "Urahara"];
  58.                         var html="";
  59.                        
  60.                         html+="<h4>One Piece Character:</h4><br />";
  61.                        
  62.                         html+="<ul>";
  63.                        
  64.                         for(var i=0; i<member.length; i++){
  65.                                 html+="<li>"+member[i]+"</li>";
  66.                         }
  67.                        
  68.                         html+="</ul>";
  69.                        
  70.                         document.getElementById("result").innerHTML=html;
  71.                 break;
  72.         }
  73. }

In the code above we just created only one method called dropdownList(), this function will dynamically change your html list data base on the dropdown option. This function uses switch statement that will allow you to easily switch between cases. The trick on this code is that whenever the dropdown button select an option it will dynamically populate the html list by appending the data.

Output:

The How to Change List Value Base on Dropdown in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Change List Value Base on Dropdown in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment