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

Dynamically Add and Remove Form Item using jQuery clone() Method Tutorial

nahar122

Lead Magnet Creator
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
175
Likes
131
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this article, I will be discussing about how to create an Add and Remove Form Item Feature in web-application using jQuery's clone()

method
. This kind of feature is commonly applied to a project which has a process of saving multiple data at once in the database. There are lots of ideas and techniques you can create to achieve this feature with the help of jQuery, but I found this solution more simple and easiest in my experience.

Below, I have provided a simple PHP Web Application which contains a form for Contact Directories which allows the users to add multiple set of input dynamically. The form items/sets contain the fields that are needed and each of these items has a 'remove button' using an 'x' icon button at the upper-right. The remove button allows the user to remove the item. It has also contained some features that are commonly included in submitting a form such as preventing submitting the form when no item is listed and if required fields are empty. The application only displays the submitted data and there is no database included.

Getting Started

To run the application you must have a local server such as XAMPP in order to run PHP Scripts in your device.

Using XAMPP:
Download and Install XAMPP. After that, open the XAMPP's Control Panel and start the Apache. Then create a new directory in your xampp/htdocs directory.

Libraries/Plugin used:
(Download and compile the ff in your source code directory)

Creating Default Page Interface

The script below is the code of our default page which is the index.php. This file contains the contact form.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >

  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Cloning Element</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "./Font-Awesome-master/css/all.min.css"

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/bootstrap.min.css"

    >
  10. <script

    src

    =

    "./js/jquery-3.6.0.min.js"

    ></

    script

    >
  11. <script

    src

    =

    "./js/bootstrap.min.js"

    ></

    script

    >
  12. <script

    src

    =

    "./js/script.js"

    ></

    script

    >
  13. <style

    >
  14. :root {
  15. --bs-success-rgb: 71, 222, 152 !important;
  16. }

  17. html,
  18. body {
  19. height: 100%;
  20. width: 100%;
  21. font-family: Apple Chancery, cursive;
  22. }

  23. .btn-info.text-light:hover,
  24. .btn-info.text-light:focus {
  25. background: #000;
  26. }
  27. </

    style

    >
  28. </

    head

    >

  29. <body

    class

    =

    "bg-light"

    >
  30. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-dark bg-gradient"

    id

    =

    "topNavBar"

    >
  31. <div

    class

    =

    "container"

    >
  32. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  33. Sourcecodester
  34. </

    a

    >

  35. <div

    >
  36. <b

    class

    =

    "text-light"

    >

    Cloning Element</

    b

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    nav>
  40. <div

    class

    =

    "container py-5"

    id

    =

    "page-container"

    >
  41. <div

    class

    =

    "card rounded-0 shadow"

    >
  42. <div

    class

    =

    "card-header"

    >
  43. <h4

    class

    =

    "card-title"

    >

    Simple Contact Information Form</

    h4

    >
  44. </

    div

    >
  45. <div

    class

    =

    "card-body"

    >
  46. <form

    action

    =

    "submitted_data.php"

    method

    =

    "POST"

    id

    =

    "contact-form"

    >
  47. <div

    class

    =

    "row align-items-center"

    id

    =

    "item-card"

    >
  48. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 item mb-3 h-100"

    >
  49. <div

    class

    =

    "card rounded-0 shadow h-100"

    >
  50. <div

    class

    =

    "card-body h-100"

    >
  51. <div

    class

    =

    "container-fluid"

    >
  52. <div

    class

    =

    "w-100 text-end"

    >
  53. <button

    class

    =

    "btn btn-outline-danger btn-sm rounded-0 remove-item"

    type

    =

    "button"

    ><i

    class

    =

    "fa fa-times"

    ></

    i

    ></

    button

    >
  54. </

    div

    >
  55. <div

    class

    =

    "form-group"

    >
  56. <label

    class

    =

    "control-label text-muted"

    >

    Name</

    label

    >
  57. <input

    type

    =

    "text"

    name

    =

    "name[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  58. </

    div

    >
  59. <div

    class

    =

    "form-group"

    >
  60. <label

    class

    =

    "control-label text-muted"

    >

    Email</

    label

    >
  61. <input

    type

    =

    "email"

    name

    =

    "email[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  62. </

    div

    >
  63. <div

    class

    =

    "form-group"

    >
  64. <label

    class

    =

    "control-label text-muted"

    >

    Contact #</

    label

    >
  65. <input

    type

    =

    "text"

    name

    =

    "contact[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  66. </

    div

    >
  67. <div

    class

    =

    "form-group"

    >
  68. <label

    class

    =

    "control-label text-muted"

    >

    Address</

    label

    >
  69. <textarea

    name

    =

    "address[]"

    rows

    =

    "3"

    style

    =

    "resize:none"

    class

    =

    "form-control form-control-sm rounded-0"

    required></

    textarea

    >
  70. </

    div

    >
  71. </

    div

    >
  72. </

    div

    >
  73. </

    div

    >
  74. </

    div

    >
  75. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 h-100"

    id

    =

    "new_item"

    >
  76. <div

    class

    =

    "card rounded-0 shadow h-100"

    >
  77. <div

    class

    =

    "card-body h-100"

    >
  78. <div

    class

    =

    "container-fluid h-100"

    >
  79. <a

    href

    =

    "javascript:void(0)"

    id

    =

    "new-item-btn"

    class

    =

    "text-dark fw-bolder fs-1 h-100 w-100 text-decoration-none"

    >
  80. <div

    class

    =

    "d-flex w-100 h-100 align-items-center justify-content-center"

    >
  81. <i

    class

    =

    "fa fa-plus"

    ></

    i

    >
  82. </

    div

    >
  83. </

    a

    >
  84. </

    div

    >
  85. </

    div

    >
  86. </

    div

    >
  87. </

    div

    >
  88. </

    div

    >
  89. <div

    class

    =

    "row justify-content-center"

    >
  90. <button

    class

    =

    "btn btn-primary bg-gradient col-md-4 btn-large rounded-pill"

    type

    =

    "button"

    id

    =

    "submit-form"

    >

    Submit Data</

    button

    >
  91. </

    div

    >
  92. </

    form

    >
  93. </

    div

    >
  94. </

    div

    >
  95. </

    div

    >
  96. </

    body

    >
  97. <!--The ELement to be Cloned -->
  98. <noscript

    id

    =

    "el-clone"

    >
  99. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 item mb-3 h-100"

    >
  100. <div

    class

    =

    "card rounded-0 shadow h-100"

    >
  101. <div

    class

    =

    "card-body h-100"

    >
  102. <div

    class

    =

    "container-fluid"

    >
  103. <div

    class

    =

    "w-100 text-end"

    >
  104. <button

    class

    =

    "btn btn-outline-danger btn-sm rounded-0 remove-item"

    type

    =

    "button"

    ><i

    class

    =

    "fa fa-times"

    ></

    i

    ></

    button

    >
  105. </

    div

    >
  106. <div

    class

    =

    "form-group"

    >
  107. <label

    class

    =

    "control-label text-muted"

    >

    Name</

    label

    >
  108. <input

    type

    =

    "text"

    name

    =

    "name[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  109. </

    div

    >
  110. <div

    class

    =

    "form-group"

    >
  111. <label

    class

    =

    "control-label text-muted"

    >

    Email</

    label

    >
  112. <input

    type

    =

    "email"

    name

    =

    "email[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  113. </

    div

    >
  114. <div

    class

    =

    "form-group"

    >
  115. <label

    class

    =

    "control-label text-muted"

    >

    Contact #</

    label

    >
  116. <input

    type

    =

    "text"

    name

    =

    "contact[]"

    class

    =

    "form-control form-control-sm rounded-0"

    required>
  117. </

    div

    >
  118. <div

    class

    =

    "form-group"

    >
  119. <label

    class

    =

    "control-label text-muted"

    >

    Address</

    label

    >
  120. <textarea

    name

    =

    "address[]"

    rows

    =

    "3"

    style

    =

    "resize:none"

    class

    =

    "form-control form-control-sm rounded-0"

    required></

    textarea

    >
  121. </

    div

    >
  122. </

    div

    >
  123. </

    div

    >
  124. </

    div

    >
  125. </

    div

    >
  126. </

    noscript

    >
  127. <!--The ELement to be Cloned -->

  128. </

    html

    >

Creating Submitted Data Display Page

This is the page where the page will be redirected when the form is submitted. It contains a table that list all the contact information submitted. This page can be only browse when the form is submitted. Save the scrip below as submitted_data.php.

  1. <?php
  2. //

    Checks if There is a POST Data
  3. if(

    $_SERVER[

    'REQUEST_METHOD'

    ]

    !=

    'POST'

    )

    {
  4. echo '<script>

    alert("No data has been submitted."); location.replace("./")</

    script

    >

    ';
  5. }else{
  6. extract($_POST);
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html

    lang

    =

    "en"

    >

  11. <head

    >
  12. <meta

    charset

    =

    "UTF-8"

    >
  13. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  14. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  15. <title

    >

    Cloning Element</

    title

    >
  16. <link

    rel

    =

    "stylesheet"

    href

    =

    "./Font-Awesome-master/css/all.min.css"

    >
  17. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/bootstrap.min.css"

    >
  18. <script

    src

    =

    "./js/jquery-3.6.0.min.js"

    ></

    script

    >
  19. <script

    src

    =

    "./js/bootstrap.min.js"

    ></

    script

    >
  20. <script

    src

    =

    "./js/script.js"

    ></

    script

    >
  21. <style

    >
  22. :root {
  23. --bs-success-rgb: 71, 222, 152 !important;
  24. }

  25. html,
  26. body {
  27. height: 100%;
  28. width: 100%;
  29. font-family: Apple Chancery, cursive;
  30. }

  31. .btn-info.text-light:hover,
  32. .btn-info.text-light:focus {
  33. background: #000;
  34. }
  35. </

    style

    >
  36. </

    head

    >

  37. <body

    class

    =

    "bg-light"

    >
  38. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-dark bg-gradient"

    id

    =

    "topNavBar"

    >
  39. <div

    class

    =

    "container"

    >
  40. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  41. Sourcecodester
  42. </

    a

    >

  43. <div

    >
  44. <b

    class

    =

    "text-light"

    >

    Cloning Element</

    b

    >
  45. </

    div

    >
  46. </

    div

    >
  47. </

    nav>
  48. <div

    class

    =

    "container py-5"

    id

    =

    "page-container"

    >
  49. <div

    class

    =

    "card rounded-0 shadow"

    >
  50. <div

    class

    =

    "card-header"

    >
  51. <div

    class

    =

    "d-flex w-100"

    >
  52. <h4

    class

    =

    "card-title col-8"

    >

    Submitted Data</

    h4

    >
  53. <div

    class

    =

    "col-4 text-end"

    >
  54. <a

    class

    =

    "btn btn-light rounded-0 border btn-sm"

    href

    =

    "./"

    ><i

    class

    =

    "fa fa-angle-left"

    ></

    i

    >

    Back</

    a

    >
  55. </

    div

    >
  56. </

    div

    >
  57. </

    div

    >
  58. <div

    class

    =

    "card-body"

    >
  59. <div

    class

    =

    "container-fluid"

    >
  60. <table

    class

    =

    "table table-stripped table-bordered"

    >
  61. <colgroup

    >
  62. <col

    width

    =

    "5%"

    >
  63. <col

    width

    =

    "25%"

    >
  64. <col

    width

    =

    "15%"

    >
  65. <col

    width

    =

    "15%"

    >
  66. <col

    width

    =

    "40%"

    >
  67. </

    colgroup

    >
  68. <thead

    >
  69. <tr

    class

    =

    "bg-primary bg-gradient text-light"

    >
  70. <th

    class

    =

    "text-center"

    >

    #</

    th

    >
  71. <th

    class

    =

    "text-center"

    >

    Name</

    th

    >
  72. <th

    class

    =

    "text-center"

    >

    Email</

    th

    >
  73. <th

    class

    =

    "text-center"

    >

    Contact #</

    th

    >
  74. <th

    class

    =

    "text-center"

    >

    Address</

    th

    >
  75. </

    tr

    >
  76. </

    thead

    >
  77. <tbody

    >
  78. <?php
  79. $i =

    1

    ;
  80. //

    Looping all the submitted data
  81. foreach(

    $name as $k =

    >

    $v):
  82. ?>
  83. <tr

    >
  84. <td

    class

    =

    "px-2 py-1 text-center"

    ><?=

    $i++; ?></

    td

    >
  85. <td

    class

    =

    "px-2 py-1"

    ><?=

    $v ?></

    td

    >
  86. <td

    class

    =

    "px-2 py-1"

    ><?=

    $email[

    $k]

    ?></

    td

    >
  87. <td

    class

    =

    "px-2 py-1"

    ><?=

    $contact[

    $k]

    ?></

    td

    >
  88. <td

    class

    =

    "px-2 py-1"

    ><?=

    $address[

    $k]

    ?></

    td

    >
  89. </

    tr

    >
  90. <?php endforeach; ?>
  91. </

    tbody

    >
  92. </

    table

    >
  93. </

    div

    >
  94. </

    div

    >
  95. </

    div

    >
  96. </

    div

    >
  97. </

    body

    >
  98. </

    html

    >

Creating Main Function

The script below is a JavaScript file that contains our jQuery custom functions and event codes. The script includes the codes of cloning an item/set to be added at the form, removing the item/set from the form, and the form check process before submitting the data. Save the file as script.js and in my case, my CSS and JS files are organized by directory which makes this file located inside the JS directory.

  1. // removing the contact Item
  2. function

    remove_item(

    _this)

    {
  3. _this.closest

    (

    '.item'

    )

    .remove

    (

    )
  4. }
  5. $(

    function

    (

    )

    {
  6. // Focuses the first Input of Form
  7. $(

    'input[name="name[]"]'

    )

    .first

    (

    )

    .focus

    (

    )

  8. // Event Lister of Form Item Remove Button when Click
  9. $(

    '.remove-item'

    )

    .click

    (

    function

    (

    )

    {
  10. remove_item(

    $(

    this

    )

    )
  11. }

    )

  12. // Adds New Form Item when Add Item has been Clicked
  13. $(

    '#new-item-btn'

    )

    .click

    (

    function

    (

    )

    {
  14. // cloning the item card from noscript element
  15. var

    item =

    $(

    $(

    'noscript#el-clone'

    )

    .html

    (

    )

    )

    .clone

    (

    )

  16. // Inserting the new item befor 'Add Item' Card
  17. $(

    '#new_item'

    )

    .before

    (

    item)

  18. // Focuses the first input in the added item card
  19. item.find

    (

    'input[name="name[]"]'

    )

    .focus

    (

    )

  20. // Event Lister of added item card Remove Button when Click
  21. item.find

    (

    '.remove-item'

    )

    .click

    (

    function

    (

    )

    {
  22. remove_item(

    $(

    this

    )

    )
  23. }

    )
  24. }

    )

  25. // Event when clicking the Submit Button
  26. $(

    '#submit-form'

    )

    .click

    (

    function

    (

    )

    {
  27. var

    form =

    $(

    '#contact-form'

    )
  28. $(

    this

    )

    .attr

    (

    'disabled'

    ,

    true

    )
  29. .text

    (

    "Submitting Data ..."

    )
  30. if

    (

    $(

    '#item-card .item'

    )

    .length

    <=

    0

    )

    {
  31. // Checks if there is atleast 1 Item Card
  32. alert(

    "No Contact Item Added."

    )
  33. $(

    this

    )

    .attr

    (

    'disabled'

    ,

    false

    )
  34. .text

    (

    "Submit Data"

    )
  35. return

    false

    ;
  36. }

    else

    if

    (

    form[

    0

    ]

    .checkValidity

    (

    )

    ==

    false

    )

    {
  37. // Checks if All input fields are filled
  38. form[

    0

    ]

    .reportValidity

    (

    )
  39. $(

    this

    )

    .attr

    (

    'disabled'

    ,

    false

    )
  40. .text

    (

    "Submit Data"

    )
  41. return

    false

    ;
  42. }

    else

    {
  43. // Form Submission
  44. form.submit

    (

    )
  45. }
  46. }

    )
  47. }

    )

There you go. You can now test the application on your end and check if it working properly and achieves the goal I stated above. If there's an error occurred on your end, review your codes with the codes I provided above. You can also download the source code I created for this tutorial. The download link/button is located after this article.

That ends this tutorial. I hope this will help you with what you are looking for and you'll find it useful with your current or future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Related Links:
  • JavaScript Tutorial
  • JavaScript Source Codes Free Download

Happy Coding :)


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

442,401

317,942

317,951

Top