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

JavaScript - Submit Multiple Fields

nilinia

Cryptographic Vulnerability Auditor
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
119
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Submit Multiple Fields using JavaScript. This code will dynamically insert a multiple you array of form fields when the user click the insert button. The code use onclick() to call a method that can insert a multiple form fields with the use of for() loop to repeatedly insert the inputs base on the length of the array index. 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 - Submit Multiple Fields</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-5"

    >
  18. <button

    type

    =

    "button"

    class

    =

    "btn btn-success"

    onclick

    =

    "addField();"

    >

    Add Field</

    button

    >
  19. <br

    /

    ><br

    /

    >
  20. <form

    method

    =

    "POST"

    >
  21. <div

    id

    =

    "forms"

    class

    =

    "form-inline"

    >
  22. <input

    type

    =

    "text"

    class

    =

    "form-control fields"

    placeholder=

    "Enter Full Name"

    >
  23. </

    div

    >
  24. <br

    /

    >
  25. <center

    ><button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "submitForm();"

    >

    Insert</

    button

    ></

    center

    >
  26. </

    form

    >
  27. </

    div

    >
  28. <div

    class

    =

    "col-md-7"

    >
  29. <table

    class

    =

    "table table-bordered"

    >
  30. <thead

    class

    =

    "alert-info"

    >
  31. <tr

    >
  32. <th

    >

    Full Name</

    th

    >
  33. </

    tr

    >
  34. </

    thead

    >
  35. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  36. </

    table

    >
  37. </

    div

    >
  38. </

    div

    >
  39. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  40. </

    body

    >
  41. </

    html

    >

Creating the Script

This code contains the script of the application. This code will insert a multiple form field when the button is clicked. 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. var

    fieldId =

    0

    ;
  2. var

    names =

    [

    ]

    ;

  3. displayAll(

    )

    ;

  4. function

    displayAll(

    )

    {
  5. var

    data =

    ''

    ;
  6. if

    (

    names.length

    >

    0

    )

    {
  7. for

    (

    i =

    0

    ;

    i <

    names.length

    ;

    i++

    )

    {
  8. data +=

    '<tr>'

    ;
  9. data +=

    '<td>'

    +

    names[

    i]

    +

    '</td>'

    ;
  10. data +=

    '</tr>'

    ;
  11. }
  12. }

  13. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  14. }

    ;


  15. function

    addElement(

    parentId,

    elementTag,

    elementId,

    html)

    {
  16. var

    id =

    document.getElementById

    (

    parentId)

    ;
  17. var

    newElement =

    document.createElement

    (

    elementTag)

    ;
  18. newElement.setAttribute

    (

    'id'

    ,

    elementId)

    ;
  19. newElement.innerHTML

    =

    html;
  20. id.appendChild

    (

    newElement)

    ;

  21. }

  22. function

    removeField(

    elementId)

    {
  23. var

    fieldId =

    "field-"

    +

    elementId;
  24. var

    element =

    document.getElementById

    (

    fieldId)

    ;
  25. element.parentNode

    .removeChild

    (

    element)

    ;
  26. }

  27. function

    addField(

    )

    {
  28. fieldId++;
  29. var

    html =

    '<br /><input type="text" class="form-control fields" placeholder="Enter Full Name">'

    +

    '<button class="btn btn-danger btn-sm" onclick="removeField('

    +

    fieldId+

    ');"><span class="glyphicon glyphicon-minus"></span></button><br />'

    ;
  30. addElement(

    'forms'

    ,

    'div'

    ,

    'field-'

    +

    fieldId,

    html)

    ;
  31. }

  32. function

    submitForm(

    )

    {
  33. var

    inputs=

    document.getElementsByClassName

    (

    'fields'

    )

    ;

  34. for

    (

    var

    i=

    0

    ;

    i<

    inputs.length

    ;

    i++

    )

    {
  35. var

    val=

    inputs[

    i]

    .value

    ;
  36. names.push

    (

    val.trim

    (

    )

    )

    ;
  37. inputs[

    i]

    .value

    =

    ''

    ;
  38. }

  39. displayAll(

    )

    ;
  40. }

There you have it we successfully created a Submit Multiple Fields 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,341

323,350

Top