Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Skip to main content

Dynamically Adding Form Elements in JQuery

Dynamically Adding Form Elements in JQuery
Back to insights

In my last project, my client wanted the ability to add multiple form elements to their existing forms.  This was relatively easy using JQuery and JavaScript. This tutorial will show you how to dynamically add, delete and save the form elements.

The Code

First things is to build out the HTML file with a form.

Dynamically Adding Form Elements in JQuery - 1

This is the output from the HTML above. Note that the ‘save button’ does not show because I didn’t want the user to click on the button with nothing to save.

When the document loads it hides the save button and initializes a counter variable called ‘x’. This counter keeps track of how many form elements there are.

When the user clicks on the ‘add row’ button it appends a first name, last name and a delete button.

Dynamically Adding Form Elements in JQuery - 2

Below shows that I’m appending a new form element to the div named ‘input_wrapper’.

Dynamically Adding Form Elements in JQuery - 3

HTML view

Dynamically Adding Form Elements in JQuery - 4

For the delete functionality, every time the user clicks on a delete button, I’m targeting the button’s class, ‘deleteBtn’. From that click, I can get access to the event object. In that event object, I can look for the id which is the counter, ‘x’.  Then I can remove that form element based on the id associated with the div.

Dynamically Adding Form Elements in JQuery - 5

There are many ways you can save a form, but I decided to use a button. First, I serialized the form based on the input’s name attribute.

Dynamically Adding Form Elements in JQuery - 6

Secondly, if the form has more than one element, you can push it into an array and submit. Otherwise, for the one form element, you can set the object and submit.

Dynamically Adding Form Elements in JQuery - 7

Hope you found this tutorial helpful and useful. Click here for the working demo.

Digging In

  • Software Engineering

    Enhancing the Ferguson Counter Experience: A Frontline Tool Built Around Associate Expertise and Customer Relationships

  • Software Engineering

    What Does Quality Mean to You?

    About a year ago, I was in a conversation with one of our clients when they asked me this question: “What does quality mean to you?” I gave an answer in the moment, but a better answer came to me later. As a developer, the easy answer is something about unit tests, code coverage, and bug-free releases. That’s where my brain went first. But the question stayed with […]

  • Software Engineering

    When There’s Too Much to Fix: How Smart Prioritization Unlocks Revenue at Scale

    Every operations team has a backlog. The question isn’t whether you can clear it — it’s whether you’re clearing it in the right order. For most teams, the honest answer is no. And that gap between the order work gets done, and the order it should get done is quietly costing organizations millions. The Volume Problem High-volume exception processing shows up across […]

  • Software Engineering

    Creating Reusable Code Templates to Reduce Client Project Startup Time

    In consulting, one of the least visible but most expensive phases of a project is the beginning. Teams can spend days or weeks setting up repositories, agreeing on structure, wiring basic infrastructure, and solving problems that have already been solved many times before. Code templates are a practical way to reduce overhead while improving consistency. […]

  • Software Engineering

    Player Three Has Entered the Game: How AI Is Finally Bridging the Divide Between Design and Engineering

    As AI begins to become more prominent in our day-to-day lives, I find myself in a unique position. As a practicing software engineer and UI/UX designer, I am genuinely happy to see the introduction of AI tools begin to take shape in our industry. But more importantly, I am happy to start seeing the effects it is having on what has historically been a pretty challenging relationship: the […]

  • Software Engineering

    The Disappearing Middle of Software Work: Why the Bookends – Strategy & Impact – Matter Most Now

    Here’s a question nobody in enterprise software wants to sit with: what happens to the middle? Not the middle of the org chart. The middle of the work. The vast, expensive layer of effort that has defined enterprise software delivery for thirty years—translating what the business wants into working code. The requirements-to-implementation pipeline. The “build phase.” […]