Add/Remove Input Fields Dynamically with jQuery

If you are looking to add and remove duplicate input fields, here’s another jQuery example below to do the task for you. This jQuery snippet adds duplicate input fields dynamically and stops when it reaches maximum.

add-remove-fields

If you read comment lines carefully, the process is pretty straight forward. We start with 1 input field and let user add more fields until the count reaches maximum. Same process goes to delete button, when clicked, it removes current text field by removing the parent element, which is div element.

JQUERY
12345678910111213141516171819
$(document).ready(function() {
	var max_fields      = 10; //maximum input boxes allowed
	var wrapper   		= $(".input_fields_wrap"); //Fields wrapper
	var add_button      = $(".add_field_button"); //Add button ID
	var x = 1; //initlal text box count
	$(add_button).click(function(e){ //on add input button click
		e.preventDefault();
		if(x < max_fields){ //max input box allowed
			x++; //text box increment
			$(wrapper).append('

<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>
'); //add input box
		}
	});
	$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
		e.preventDefault(); $(this).parent('div').remove(); x--;
	})
});

HTML Code

Here’s HTML code you need to place within BODY tag of your page, perhaps within a FORM tag.

HTML
123456

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
<div><input type="text" name="mytext[]"></div>
</div>

Demo

We start with single Input field and a “Add more Field” button to allow user to add more fields. The text input field is wrapped in div element, as explained above, on delete button click, it finds parent element, which is a div and removes it.
Another example can be found here Add and Remove Fields Dynamic and Simple with jQuery.

Capture array values using PHP from these input fields in next post.

  • 218 Comments

    Add Comment
    • Vamshikrishna akula
      thanks for the code...my requirement is to add array(controller) values one by one to the dynamically generated feilds... i am strucking with these could you please help me anyone thankyou
    • Kavali.saiprasad
      how to do when click on remove only remove clicked one not count --
    • Rico
      Hii, can u help me? if add more field can u show count 1, 2, 3, etc. inside the field
    • Rapsan Jani
      Great tutorial... Easy to understand & learn. Can you share How to insert multiple data in PHP using add remove input field in jquery
    • Rapsan Jani
      Great tutorial... Easy to understand & learn. Can you share How to insert multiple data in PHP using <a href="https://codingstatus.com/how-to-add-and-remove-input-fields-dynamically-using-jquey/&quot; add remove input field in jquery
    • John
      Great Article ! I have just read this article and it helps me alot in my current project . Thanks for posting valuable article . Keep its up good work !
    • Sazid Hasan Milon
      Thank you for your nice post. How can I use this code to add an invoice field like this (http://prntscr.com/rmqmae)? I just want to create this invoice and want to add the "add more field" option here with multiple inputs. Please, can you help me with this? and also please let me know about the data bypass system. Thank you Sazid Hasan Milon http://www.shmilon.com
    • Ganesh
      how to access the values in newly created form inputs?
      • Andràs Marghescu Ganesh
        Hi, same question for me. The script adds a field as expected but the fields name will stay the same. I thought adding the Count Variable 'x' to the new fields name but could not solve it. I tried: $(wrapper).append('<div><input type="text" name="mytext[]'+x'"/><a href="#" class="remove_field">Remove</a></div>'); //add input box Maybe somebody has an idea?
    • Sanjay
      the input field has to take auto incerement serial number how i have to do
      • Saran Sanjay
        12345678910111213141516171819
        var max_fields      = 10; //maximum input boxes allowed
        	var wrapper   		= $(&quot;.input_fields_wrap&quot;); //Fields wrapper
        	var add_button      = $(&quot;.add_field_button&quot;); //Add button ID
        	
        	var x = 1; //initlal text box count
        	$(add_button).click(function(e){ //on add input button click
        		e.preventDefault();
        		if(x &lt; max_fields){ //max input box allowed
        			x++; //text box increment
        			$(wrapper).append(&#039;&lt;div&gt;&lt;input type=&quot;text&quot; name=&quot;mytext[]&quot; value=&quot;&#039;+ x +&#039;&quot; /&gt;&lt;a href=&quot;#&quot; class=&quot;remove_field&quot;&gt;Remove&lt;/a&gt;&lt;/div&gt;&#039;); //add input box
        		}
        	});
        	
        	$(wrapper).on(&quot;click&quot;,&quot;.remove_field&quot;, function(e){ //user click on remove text
        		e.preventDefault(); $(this).parent(&#039;div&#039;).remove(); x--;
            $( &#039;input[name=&quot;mytext[]&quot;]&#039; ).each(function(index) {
            		$( this ).val(index+1);
            });
        	})
        Here's jsfiddle example : https://jsfiddle.net/saaraan/948cbn52/
    • Attaullah Shah
      Simple and elegant