Master Dynamic Form Fields in JavaScript with Ease

We will build a form where you can add a new input field or remove them anytime. It is super useful for dynamic forms.

When I click on Add field a new input should appear, and when I click on Remove then that particular input field to which this button is attached should get disappeared.

You will learn how to work with DOM manipulation and how to do the append work.

JavaScript Dynamic Form Fields: Markup

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 1

 

I have a heading with content as Dynamic Form Fields. After that we are having a main div. Inside this we are having an id as field-container. I

nside of this we are again having a div whose class is field-wrapper. Inside of it we are having an input and a button.

The input type is text with a placeholder, and the class of the button is remove-button. In the end we are having an Add field button whose id is add.

HTML structure

<h1>Dynamic Form Fields</h1>

<div id="field-container">
  <div class="field-wrapper">
    <input type="text" placeholder="Enter something" />
    <button class="remove-button">Remove</button>
  </div>
</div>

<button id="add">Add field</button>

JavaScript Dynamic Form Fields: Selecting elements

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 3

 

First we will get the container and the Add button.

const fieldContainer = document.getElementById('field-container');
const addButton = document.getElementById('add');

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 4

 

JavaScript Dynamic Form Fields: Add field logic

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 5

Make an event listener to the Add button such that whenever you click on Add field a new input field will appear.

  • Create a wrapper div.
  • Add the field-wrapper class to it.
  • Create a new input and set its type and placeholder.
  • Create a new Remove button and add the remove-button class.
  • Append the input and button to the wrapper.
  • Append the wrapper to the main container.
  • Add a click listener to the newly created Remove button to remove its wrapper. 

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 11

Master Dynamic Form Fields in JavaScript with Ease screenshot 10

 

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 9

 

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 8

 

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 7

 

addButton.addEventListener('click', () => {
  // Create wrapper
  const wrapper = document.createElement('div');
  wrapper.classList.add('field-wrapper');


  // Create input
  const newInput = document.createElement('input');
  newInput.type = 'text';
  newInput.placeholder = 'Enter something anything which you want';

  // Create remove button
  const removeButton = document.createElement('button');
  removeButton.innerText = 'Remove';
  removeButton.classList.add('remove-button');

  // Append to wrapper
  wrapper.appendChild(newInput);
  wrapper.appendChild(removeButton);

  // Append wrapper to main container
  fieldContainer.appendChild(wrapper);

  // Click listener for this remove button
  removeButton.addEventListener('click', () => {
    fieldContainer.removeChild(wrapper);
  });
});

JavaScript Dynamic Form Fields: Remove field logic for existing buttons

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 14

Attach an event listener to the existing Remove button that is already on the page.

We are selecting all remove buttons and applying this functionality, taking the wrapper from the parent element and then removing it.

document.querySelectorAll('.remove-button').forEach(button => {
  button.addEventListener('click', e => {
    const wrapper = e.target.parentElement;
    fieldContainer.removeChild(wrapper);
  });
});

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 15

 

Complete code for JavaScript Dynamic Form Fields

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 17

 

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Dynamic Form Fields</title>
    <style>
      .field-wrapper {
        margin-bottom: 8px;
        display: flex;
        gap: 8px;
      }
    </style>
  </head>
  <body>
    <h1>Dynamic Form Fields</h1>
    <div id="field-container">
      <div class="field-wrapper">
        <input type="text" placeholder="Enter something" />
        <button class="remove-button">Remove</button>
      </div>
    </div>

    <button id="add">Add field</button>

    <script>
      const fieldContainer = document.getElementById('field-container');
      const addButton = document.getElementById('add');

      addButton.addEventListener('click', () => {
        const wrapper = document.createElement('div');
        wrapper.classList.add('field-wrapper');

        const newInput = document.createElement('input');
        newInput.type = 'text';
        newInput.placeholder = 'Enter something anything which you want';

        const removeButton = document.createElement('button');
        removeButton.innerText = 'Remove';
        removeButton.classList.add('remove-button');

        wrapper.appendChild(newInput);
        wrapper.appendChild(removeButton);

        fieldContainer.appendChild(wrapper);

        removeButton.addEventListener('click', () => {
          fieldContainer.removeChild(wrapper);
        });
      });

      document.querySelectorAll('.remove-button').forEach(button => {
        button.addEventListener('click', e => {
          const wrapper = e.target.parentElement;
          fieldContainer.removeChild(wrapper);
        });
      });
    </script>
  </body>
</html>

Troubleshooting: ID or class mismatch

 

Master Dynamic Form Fields in JavaScript with Ease screenshot 16

 

I wrote the wrong spelling of the ID and corrected it. If it is not working, copy the exact id and class names from your HTML and paste them into your JavaScript so they match.

Final Thoughts

It is creating a new field and you can write whatever you want. It is also having a Remove button, and when I click on Remove, that particular field gets removed. Add field, remove field, and repeat. Earlier we were adding an event listener to the Add button, and then we added the functionality to the Remove button for every button on the page.

Leave a Comment