Add Remove Multiple Input Fields Dynamically Using jQuery with Example

calendar_month 09 May 2022 | jQuery

In this tutorial, you will learn how to Add Remove multiple Input fields Dynamically using jQuery with example. Many times, we need multiple input fields in a form to fetch multiple items, for example, multiple mobile numbers, multiple email ids, etc., so we require multiple fields.

Therefore, we can easily add or remove multiple input fields using jQuery.

In the following example, you'll see how to dynamically add or remove multiple input fields using Bootstrap 4 and jQuery. You can simply add more input fields by clicking the "add more input" button and remove any input field by clicking the Remove button displayed in front of the input field.



Step 1: Create HTML File

First, we need to create an HTML file and add bootstrap CSS and JS links, then create a form like this.


<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Dynamically Add Or Remove Inputs</title>
<div class="container mt-5">
  <div class="row">
    <div class="col-md-12 mb-4 text-center">
      <h5>Dynamically Add or Remove Input Fields Using Jquery</h5>
    <div class="col-md-3"></div>
    <div class="col-md-6">
	    		<div class="mb-3 input-group repeatDiv" id="repeatDiv">
	    			<input type="text" class="form-control" name="mobile[]" placeholder="Enter Title">

	    		<button type="button" class="btn btn-info" id="repeatDivBtn" data-increment="1">Add More Input</button>
    <div class="col-md-3"></div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
	<script src=""></script>
    <script type="text/javascript" src="script.js"></script>

Step 2: Create JavaScript File

In this step, we need to create a JavaScript file to write jQuery code to dynamically add or remove multiple input fields.


$(document).ready(function () {

	$("#repeatDivBtn").click(function () {

		$newid = $(this).data("increment");
		$repeatDiv = $("#repeatDiv").wrap('<div/>').parent().html();
		$(".repeatDiv").last().attr('id',   "repeatDiv" + '_' + $newid);
		$("#repeatDiv" + '_' + $newid).append('<div class="input-group-append"><button type="button" class="btn btn-danger removeDivBtn" data-id="repeatDiv'+'_'+ $newid+'">Remove</button></div>');
		$(this).data("increment", $newid);


	$(document).on('click', '.removeDivBtn', function () {

		$divId = $(this).data("id");
		$inc = $("#repeatDivBtn").data("increment");
		$("#repeatDivBtn").data("increment", $inc-1);



Now the above code is ready to run.

Ashwani Kumar

I'm a dedicated full-stack developer, entrepreneur, and proud owner of I reside in India, where I draw inspiration to create helpful tutorials and tips for fellow artisans. I'm deeply passionate about technologies like Bootstrap, PHP, CodeIgniter, Laravel, Node, React, JavaScript, and jQuery. I believe in working hard consistently, which keeps me motivated and driven. Challenges are opportunities to learn, setbacks are lessons. I find inspiration in the endless potential of technology and aim to make a meaningful impact with my work.

Buddy! I hope you relished the tutorial, and it was good to see you again. Keep learning. Keep visiting.

Related Tutorials
View all