• We just launched and are currently in beta. Join us as we build and grow the community.

How to Change List Value Base on Dropdown in JavaScript

ntak666

Crypto Arbitrageur
Divine
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
51
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Change List Value Base on Dropdown</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <div

    class

    =

    "form-inline"

    >
  19. <h3

    >

    Anime Title</

    h3

    >
  20. <select

    onchange

    =

    "dropdownList(this.value);"

    class

    =

    "form-control"

    >
  21. <option

    value

    =

    ""

    >

    Select an option</

    option

    >
  22. <option

    value

    =

    "dragonball"

    >

    Dragon Ball</

    option

    >
  23. <option

    value

    =

    "naruto"

    >

    Naruto</

    option

    >
  24. <option

    value

    =

    "onepiece"

    >

    One Piece</

    option

    >
  25. <option

    value

    =

    "bleach"

    >

    Bleach</

    option

    >
  26. </

    select

    >
  27. </

    div

    >
  28. </

    div

    >
  29. <div

    class

    =

    "col-md-8"

    >
  30. <div

    id

    =

    "result"

    ></

    div

    >
  31. </

    div

    >
  32. </

    div

    >
  33. <script

    src

    =

    "script.js"

    ></

    script

    >
  34. </

    body

    >
  35. </

    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. html+=

    "<h4>Dragon Ball Character:</h4><br />"

    ;

  7. html+=

    "<ul>"

    ;

  8. for

    (

    var

    i=

    0

    ;

    i<

    member.length

    ;

    i++

    )

    {
  9. html+=

    "<li>"

    +

    member[

    i]

    +

    "</li>"

    ;
  10. }

  11. html+=

    "</ul>"

    ;

  12. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;


  13. break

    ;

  14. case

    "naruto"

    :
  15. var

    member=

    [

    "Naruto"

    ,

    "Sakura"

    ,

    "Sasuke"

    ,

    "Kakashi"

    ,

    "Jiraiya"

    ]

    ;
  16. var

    html=

    ""

    ;

  17. html+=

    "<h4>Naruto Character:</h4><br />"

    ;

  18. html+=

    "<ul>"

    ;

  19. for

    (

    var

    i=

    0

    ;

    i<

    member.length

    ;

    i++

    )

    {
  20. html+=

    "<li>"

    +

    member[

    i]

    +

    "</li>"

    ;
  21. }

  22. html+=

    "</ul>"

    ;

  23. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;
  24. break

    ;

  25. case

    "onepiece"

    :
  26. var

    member=

    [

    "Luffy"

    ,

    "Sanji"

    ,

    "Zoro"

    ,

    "Nami"

    ,

    "Chopper"

    ,

    "Usopp"

    ,

    "Robin"

    ,

    "Jimbe"

    ,

    "Brook"

    ,

    "Frank"

    ]

    ;
  27. var

    html=

    ""

    ;

  28. html+=

    "<h4>One Piece Character:</h4><br />"

    ;

  29. html+=

    "<ul>"

    ;

  30. for

    (

    var

    i=

    0

    ;

    i<

    member.length

    ;

    i++

    )

    {
  31. html+=

    "<li>"

    +

    member[

    i]

    +

    "</li>"

    ;
  32. }

  33. html+=

    "</ul>"

    ;

  34. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;
  35. break

    ;

  36. case

    "bleach"

    :
  37. var

    member=

    [

    "Ichigo"

    ,

    "Rukia"

    ,

    "Inoue"

    ,

    "Sado"

    ,

    "Yoruichi"

    ,

    "Urahara"

    ]

    ;
  38. var

    html=

    ""

    ;

  39. html+=

    "<h4>One Piece Character:</h4><br />"

    ;

  40. html+=

    "<ul>"

    ;

  41. for

    (

    var

    i=

    0

    ;

    i<

    member.length

    ;

    i++

    )

    {
  42. html+=

    "<li>"

    +

    member[

    i]

    +

    "</li>"

    ;
  43. }

  44. html+=

    "</ul>"

    ;

  45. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;
  46. break

    ;
  47. }
  48. }

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:

How%20to%20Change%20List%20Value%20Base%20on%20Dropdown%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top