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

JavaScript - Change List By Drop Down

zombiebebop31

Reverse Engineer
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
137
Likes
172
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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 https://getbootstrap.com/.

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. <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"

    >

    JavaScript - Change List By Drop Down</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-3"

    >
  18. <div

    class

    =

    "form-inline"

    >
  19. <label

    >

    Select an Item</

    label

    >
  20. <select

    onchange

    =

    "changeList(this.value);"

    class

    =

    "form-control"

    >
  21. <option

    value

    =

    "math"

    >

    Math</

    option

    >
  22. <option

    value

    =

    "english"

    >

    English</

    option

    >
  23. <option

    value

    =

    "science"

    >

    Science</

    option

    >
  24. </

    select

    >
  25. </

    div

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-md-9"

    >
  28. <div

    id

    =

    "result"

    ></

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  32. </

    body

    >
  33. </

    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=

    ""

    ;

  6. html+=

    "<h4>Math Student:</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

    "english"

    :
  15. var

    member=

    [

    "Loki Odinson"

    ,

    "Peter Parker"

    ,

    "Claire Temple"

    ]

    ;
  16. var

    html=

    ""

    ;

  17. html+=

    "<h4>English Student:</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

    "science"

    :
  26. var

    member=

    [

    "Roman Atkinson"

    ,

    "Steve Roger"

    ,

    "Natasha Romanof"

    ,

    "Bruce Banner"

    ,

    "Stephen Strange"

    ]

    ;
  27. var

    html=

    ""

    ;

  28. html+=

    "<h4>Science Student:</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. }
  37. }

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!


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

452,292

323,526

323,535

Top