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

  • Digital Products

    Energy 2025 – Expansion of Fossil Fuels or Carbon Reduction?

    Now that the election is behind us, we have an opportunity to anticipate the possible effects on the energy industry under this new administration. What strategies will be impacted? What will remain the same? What opportunities can we take advantage of in 2025? This blog is meant to dig into these questions and provide some […]

  • Digital Products

    The Growing Importance of Digital Accessibility

    Embracing Digital Accessibility: A Moral and Business Imperative In today’s digital landscape, accessibility has become crucial for businesses and organizations. With increasing awareness and legal requirements, ensuring that digital products are accessible to all users, including those with disabilities, is not just a compliance issue but a moral imperative. At UDig, we champion ADA compliance […]

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]

  • Digital Products

    Mastering Legacy Application Modernization: Strategies for Success

    The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]

  • Digital Products

    CTO Confessions Podcast

    In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.

  • Digital Products

    Navigating the Challenges of On Premise to Cloud Migration

    In today’s rapidly evolving technological landscape, the shift from on premise solutions to cloud-based infrastructure has become a pivotal transformation for organizations seeking to modernize their IT operations. This transition holds the promise of increased agility, cost savings, and enhanced scalability. However, it is not without its set of formidable challenges that organizations must navigate. […]