"Add Delete Div"
Bootstrap 3.3.0 Snippet by ravindra93

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.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 ----------> <div class="container"> <div class="row"> <div class='element' id='div_1'> <div class="col-md-12"> <div class="form-group"> <div class="input-group"> <input type='text' placeholder='Enter your skill' id='txt_1' class="form-control"> <span class='btn btn-primary add input-group-addon'>Add Skill</span> </div> </div> </div> </div> </div> </div>
$(document).ready(function(){ // Add new element $(".add").click(function(){ // Finding total number of elements added var total_element = $(".element").length; // last <div> with element class id var lastid = $(".element:last").attr("id"); var split_id = lastid.split("_"); var nextindex = Number(split_id[1]) + 1; var max = 1000; // Check total number elements if(total_element < max ){ // Adding new div container after last occurance of element class $(".element:last").after("<div class='element' id='div_"+ nextindex +"'></div>"); // Adding element to <div> $("#div_" + nextindex).append(" <div class='col-md-6'> <div class='row'><div class='form-group'> <div class='input-group'><input type='text' class='form-control' placeholder='Enter your skill' id='txt_"+ nextindex +"'><span id='remove_" + nextindex + "' class='input-group-addon remove btn btn-danger'>X</span></div></div></div></div>"); } }); // Remove element $('.container').on('click','.remove',function(){ var id = this.id; var split_id = id.split("_"); var deleteindex = split_id[1]; // Remove <div> with id $("#div_" + deleteindex).remove(); }); });

Related: See More


Questions / Comments: