Add Or Remove Input Fields Dynamically with jQuery

Jan 4, 2019

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

CSS Code:

                text-align: center;
                width: 800px;
                margin: auto;
                height: 35px;
                width: 300px;
                padding: 10px;
                padding: 10px 20px;
                border: none;
                background: #036d03;
                color: #fff;
                font-weight: bold;
                background: #f95e00;
                color: #fff;
                font-weight: bold;

HTML Code:

  <div class="wrapper">
            <div id="TextBoxesGroup">
                <div class="row" id="row1">
                        <input type="text" required="" placeholder="input 1" name="pro1" class="form-control" >

        <div style="margin:auto;text-align:center">
            <div style="margin-top: 20px;">
                <input type='button' value='Add More TextBox' id='addbtntext' class="btn btn-primary" >
                <input type='button' value='Remove TextBox' id='removeButton' class="btn btn-default" >

JQuery Code:

// Add below jquery plugin in script tag

 <script src="" ></script>

           $(document).ready(function () {
                var counter = 2;
                var countergeneral = 2;
                $("#addbtntext").click(function () {

                    if (counter > 10) {
                        alert("Only 10 textboxes allow");
                        return false;

                    var newTextBoxDiv = $(document.createElement('div'))
                            .attr("class", 'row')
                            .attr("id", 'row' + counter)
                    newTextBoxDiv.html("<div><input type='text' placeholder='Input " + counter + "' name='pro' ></div>");
                $("#removeButton").click(function () {
                    if (counter == 2) {
                        alert("No more textbox to remove");
                        return false;
                    $("#row" + counter).remove();

Copyright 2018. All rights are reserved