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

Dynamically Add New Option Using JavaScript Source Code

audioanabolika

Irony Expert
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
105
Likes
79
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Dynamically Add New Option using JavaScript. This code will dynamically add new option value when the user click the insert button. The code use onclick() to call a method that will add a new option value for the select tag dynamically by providing an string for the list value. This is a free user-friendly kind of program, you can modify it and apply to your working application.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

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

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"

    >

    Dynamically Add New Option Using JavaScript Source Code</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <div

    class

    =

    "form-inline"

    >
  19. <label

    >

    Enter a category</

    label

    >
  20. <input

    type

    =

    "text"

    id

    =

    "option"

    class

    =

    "form-control"

    /

    >
  21. </

    div

    >
  22. <br

    /

    >
  23. <center

    ><button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "addOption()"

    ><span

    class

    =

    "glyphicon glyphicon-plus"

    ></

    span

    >

    Insert</

    button

    ></

    center

    >
  24. <br

    /

    >
  25. <div

    class

    =

    "form-inline"

    >
  26. <label

    >

    Select an option</

    label

    >
  27. <br

    /

    >
  28. <select

    id

    =

    "select"

    class

    =

    "form-control"

    ></

    select

    >
  29. </

    div

    >
  30. <br

    /

    >
  31. <button

    class

    =

    "btn btn-success"

    onclick

    =

    "submitData();"

    >

    Submit</

    button

    >
  32. </

    div

    >
  33. </

    div

    >
  34. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

    html

    >

Creating the Script

This code contains the script of the application. This code will dynamically add a new options when the button is clicked. 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. function

    addOption(

    )

    {
  2. var

    select =

    document.getElementById

    (

    'select'

    )

    ;
  3. var

    option =

    document.getElementById

    (

    'option'

    )

    ;

  4. if

    (

    option.value

    ==

    ""

    )

    {
  5. alert(

    "Please enter something first!"

    )

    ;
  6. }

    else

    {
  7. var

    newOption =

    document.createElement

    (

    "option"

    )

    ;
  8. var

    newOptionValue =

    document.createTextNode

    (

    option.value

    )

    ;

  9. newOption.appendChild

    (

    newOptionValue)

    ;
  10. select.insertBefore

    (

    newOption,

    select.lastChildNode

    )

    ;

  11. option.value

    =

    ""

    ;
  12. select.value

    =

    ""

    ;
  13. }
  14. }

  15. function

    submitData(

    )

    {
  16. var

    data =

    document.getElementById

    (

    'select'

    )

    .value

    ;

  17. if

    (

    data==

    ""

    )

    {
  18. alert(

    'Please select an item first'

    )

    ;
  19. }

    else

    {
  20. alert(

    'You select '

    +

    data+

    ''

    )

    ;
  21. }
  22. }

There you have it we successfully created a Dynamically Add New Option 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!

 

452,496

332,845

332,853

Top