1

Dynamically adding/removing elements in the bootstrap grid using jQuery works fine, but how can we prevent deleting the first element? Also, I want to add some limitations for adding elements. Can you help me with it? I updated with demo here

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>


<div class="container">
    
 <div class="form-group row custom-upload">

   
    <div class="col-md-12">
        <div data-role="appendRow">
            <div class="form-inline form-row">
            
              <input input type="text" class="form-control mb-2 mr-sm-2 col-sm-4" id="field-name" placeholder="Enter....">                          
                                    
                <button class="btn btn-sm btn-danger  mb-2" data-role="remove">
                 <i class="fa fa-minus"></i>
                </button>
                <button class="btn btn-sm btn-primary  mb-2" data-role="add">
                    <i class="fa fa-plus"></i>
                </button>
                
                
            </div>  
        </div> 
    </div>
 </div>
</div>
$(function() {
    // Remove button click
    $(document).on(
        'click',
        '[data-role="appendRow"] > .form-inline [data-role="remove"]',
        function(e) {
            e.preventDefault();
            $(this).closest('.form-row').remove();
        }
    );
    // Add button click
    $(document).on(
        'click',
        '[data-role="appendRow"] > .form-row [data-role="add"]',
        function(e) {
            e.preventDefault();
            var container = $(this).closest('[data-role="appendRow"]');
            new_field_group = container.children().filter('.form-row:first-child').clone();
            container.append(new_field_group);
        }
    );
});
1
  • Why are you loading two versions of Bootstrap, and why are you loading jQuery twice?
    – connexo
    Commented May 19 at 6:53

1 Answer 1

1

When removing a row, only remove elements when they are not the first child, i.e. [data-role="appendRow"] > .form-inline:not(:first-child) [data-role="remove"]:

$(function() {
// Remove button click
$(document).on(
    'click',
    '[data-role="appendRow"] > .form-inline:not(:first-child) [data-role="remove"]',
    function(e) {
        e.preventDefault();
        $(this).closest('.form-row').remove();
    }
);
// Add button click
$(document).on(
    'click',
    '[data-role="appendRow"] > .form-row [data-role="add"]',
    function(e) {
        e.preventDefault();
        if ( $('.form-row').length < 5 ) {
            var container = $(this).closest('[data-role="appendRow"]');
            new_field_group = container.children().filter('.form-row:first-child').clone();
            new_field_group.find('[data-role="remove"]').show(); // Show the Remove button for this new row

            container.append(new_field_group);
            if ( $('.form-row').length >= 5 ) {
                $('.form-row').last().find('[data-role="add"]').remove();
            }
        }
    }
);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>


<div class="container">
    
 <div class="form-group row custom-upload">

   
    <div class="col-md-12">
        <div data-role="appendRow">
            <div class="form-inline form-row">
            
              <input input type="text" class="form-control mb-2 mr-sm-2 col-sm-4" id="field-name" placeholder="Enter....">                          
                                    
                <button class="btn btn-sm btn-danger  mb-2" data-role="remove" style="display: none;">
                 <i class="fa fa-minus"></i>
                </button>
                <button class="btn btn-sm btn-primary  mb-2" data-role="add">
                    <i class="fa fa-plus"></i>
                </button>
                
                
            </div>  
        </div> 
    </div>
 </div>
</div>

10
  • thank you for the support, can we add limitations for adding elements ?
    – Jason
    Commented May 19 at 4:14
  • @user2578475 What limitations? Please be specific.
    – kmoser
    Commented May 19 at 5:13
  • I want tp add 5 elements only. can we hide the + button (add button) of last elements ?
    – Jason
    Commented May 19 at 5:21
  • 1
    @user2578475 I've updated my JS so that it only adds if there are fewer than 5 rows, and once it adds the 5th row it removes the "Add" button from it.
    – kmoser
    Commented May 19 at 5:50
  • Why are you loading two versions of Bootstrap, and why are you loading jQuery twice?
    – connexo
    Commented May 19 at 6:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.