$(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