"Add _ Delete_Divs"
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 row' id='div_1'> <div class="col-md-3"> <div class="form-group"> <label>Chapter Name</label> <input type='text' placeholder='Enter your skill' id='txt_1' class="form-control"> </div> </div> <div class="col-md-3"> <div class="form-group"> <label>Chapter Video/Url</label> <input type='text' placeholder='Enter your skill' id='txt_1' class="form-control"> </div> </div> <div class="col-md-3"> <div class="form-group"> <label>Chapter Name</label> <input type='text' placeholder='Enter your skill' id='txt_1' class="form-control"> </div> </div> <div class="col-md-3"> <button type="button" class='btn btn-primary add mt-20'>Add Chapter</button> </div> </div> <!-- Subchapter --> <div class='subelement row' id='subdiv_1'> <div class="col-md-3"> <div class="form-group"> <label>SubChapter Name</label> <input type='text' placeholder='Enter your skill' id='subtxt_1' class="form-control"> </div> </div> <div class="col-md-3"> <div class="form-group"> <label>SubChapter Video/Url</label> <input type='text' placeholder='Enter your skill' id='subtxt_2' class="form-control"> </div> </div> <div class="col-md-3"> <div class="form-group"> <label>SubChapter Name</label> <input type='text' placeholder='Enter your skill' id='subtxt_3' class="form-control"> </div> </div> <div class="col-md-3"> <button type="button" class='btn btn-primary subadd mt-20'>Add Skill</button> </div> </div> </div> </div>
.mt-20{margin-top: 20px;}
$(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='row'><div class='col-md-3'> <div class='form-group'> <label>Chapater Name</label><input type='text' class='form-control' placeholder='Enter your skill' id='txt_"+ nextindex +"'> </div></div><div class='col-md-3'><div class='form-group'> <label>Chapater Video/Url</label><input type='text' class='form-control' placeholder='Enter your skill' id='txt_"+ nextindex +"'> </div></div><div class='col-md-3'><div class='form-group'> <label>Chapater Name</label><input type='text' class='form-control' placeholder='Enter your skill' id='txt_"+ nextindex +"'> </div></div><button id='remove_" + nextindex + "' class='remove btn btn-danger mt-20'>X</button></div>"); } }); //add chapter $(".subadd").click(function(){ // Finding total number of elements added var total_element = $(".subelement").length; // last <div> with element class id var lastid = $(".subelement:last").attr("id"); var split_id = lastid.split("_"); var subindex = 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 $(".subelement:last").after("<div class='subelement' id='subdiv_"+ subindex +"'></div>"); // Adding element to <div> $("#subdiv_" + subindex).append("<div class='row'><div class='col-md-3'> <div class='form-group'> <label>SubChapater Name</label><input type='text' class='form-control' placeholder='Enter your skill' id='subtxt_"+ subindex +"'> </div></div><div class='col-md-3'><div class='form-group'> <label>SubChapater Video/Url</label><input type='text' class='form-control' placeholder='Enter your skill' id='subtxt_"+ subindex +"'> </div></div><div class='col-md-3'><div class='form-group'> <label>SubChapater Name</label><input type='text' class='form-control' placeholder='Enter your skill' id='subtxt_"+ subindex +"'> </div></div><button id='remove_" + subindex + "' class='subremove btn btn-danger mt-20'>X</button></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(); }); $('.container').on('click','.subremove',function(){ var id = this.id; var split_id = id.split("_"); var deleteindex = split_id[1]; // Remove <div> with id $("#subdiv_" + deleteindex).remove(); }); }); //main function

Related: See More


Questions / Comments: