Skip to main content

JavaScript - Change List By Drop Down

In this tutorial we will create a Change List By Drop Down using JavaScript. This code will dynamically change the list base from the selection of the select tag when the change the options. The code use onchange() to call a function that change a list base from the options of select tag using switch conditional statement, that will display a different list when the statement meet the required value. You can apply this script to your code to make your transaction faster, it 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. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">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 - Change List By Drop Down</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3">
  17. <div class="form-inline">
  18. <label>Select an Item</label>
  19. <select onchange="changeList(this.value);" class="form-control">
  20. <option value="math">Math</option>
  21. <option value="english">English</option>
  22. <option value="science">Science</option>
  23. </select>
  24. </div>
  25. </div>
  26. <div class="col-md-9">
  27. <div id="result"></div>
  28. </div>
  29. </div>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. This code will change a list base from options in select tag. 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. function changeList(item){
  2. switch(item){
  3. case "math":
  4. var member=["John Pratz", "Carl Benon"];
  5. var html="";
  7. html+="<h4>Math Student:</h4><br />";
  9. html+="<ul>";
  11. for(var i=0; i<member.length; i++){
  12. html+="<li>"+member[i]+"</li>";
  13. }
  15. html+="</ul>";
  17. document.getElementById("result").innerHTML=html;
  20. break;
  22. case "english":
  23. var member=["Loki Odinson", "Peter Parker", "Claire Temple"];
  24. var html="";
  26. html+="<h4>English Student:</h4><br />";
  28. html+="<ul>";
  30. for(var i=0; i<member.length; i++){
  31. html+="<li>"+member[i]+"</li>";
  32. }
  34. html+="</ul>";
  36. document.getElementById("result").innerHTML=html;
  37. break;
  39. case "science":
  40. var member=["Roman Atkinson", "Steve Roger", "Natasha Romanof", "Bruce Banner", "Stephen Strange"];
  41. var html="";
  43. html+="<h4>Science Student:</h4><br />";
  45. html+="<ul>";
  47. for(var i=0; i<member.length; i++){
  48. html+="<li>"+member[i]+"</li>";
  49. }
  51. html+="</ul>";
  53. document.getElementById("result").innerHTML=html;
  54. break;
  55. }
  56. }
There you have it we successfully created a Change List By Drop Down 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