Add Remove Input Fields Dynamically using jQuery

Today, We want to share with you Add Remove Input Fields Dynamically using jQuery.In this post we will show you how to add input field on button click javascript, hear for javascript add multiple input fields dynamically we will give you demo and example for implement.In this post, we will learn about add/remove multiple input fields dynamically with jquery codepen with an example.

Add Remove Input Fields Dynamically using jQuery

There are the Following The simple About dynamic form fields – add & remove multiple fields Full Information With Example and source code.

As I will cover this Post with live Working example to develop add/remove input fields dynamically with javascript, so the some major files and Directory structures for this example is following below.

Step 1: Include jQuery library

At first we need to include jQuery Latest library file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Step 2: JavaScript/jQuery Source Code:

This file All the Data contains the following JavaScript/jQuery Source codes.

$(document).ready(function(){
    var maxField = 10; 
    var addButton = $('.add_product_button'); 
    var wrapper = $('.product_sections'); 
    var fieldHTML = '<div><input type="text" name="product_name[]" value=""/><a href="javascript:void(0);" class="delete_product_button"><img src="img/remove-icon.png"/></a></div>';  
    var x = 1; 
    
    $(addButton).click(function(){
        if(x < maxField){ 
            x++; 
            $(wrapper).append(fieldHTML); 
        }
    });
    
    $(wrapper).on('click', '.delete_product_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); 
        x--; 
    });
});

Step 3: HTML Source Code:

This file contains the following HTML Markup Source codes.

  <form name="product_frm" action="" method="post">
  <div class="product_sections">
      <div>
          <input type="text" name="product_name[]" value=""/>
          <a href="javascript:void(0);" class="add_product_button" title="Add product name"><img src="img/add-icon.png"/></a>
      </div>
  </div>
  <hr/>
        <input type="submit" class="btn btn-primary" name="submit" value="SUBMIT">
    </form>

Step 4: PHP Source Code

Following PHP Source codes are used for #######.

  <?php 
    if(isset($_POST['product_name'])){
      $product_name_array = $_POST['product_name'];
      echo "<pre>";
       print_r($product_name_array);
        echo "</pre>";
    }
  ?>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to add / remove textbox dynamically with jquery.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Get country city location details
  2.   jQuery Add and Remove Multiple Div Dynamically using PHP
  3.   Add Remove multiple Input Fields Dynamically in jQuery
  4.   JavaScript Dynamic Input Form Example
  5.   jquery AJAX Dynamically Add remove PHP MySQLi
  6.   custom ajax multiple file uploading Dropzone using PHP
  7.   PHP Yii Database Connection Tutorial with Examples
  8.   Simple Vue.js 2 CRUD application
  9.   Get Laravel Route Parameters
  10.   Vuejs currency filter example