Jquery Clone() Issue
Here is my full html date and time table :
Solution 1:
I think you can solve your problem by 3 steps:
- add
class='add_time'
to your<select>
tag as below:
<select class="form-control add_time" id="add_time" name="add_time[]" class='add_time'>
get active
add_date
elements count byvar count = $('.add_button').length;
and use thecount
variable insteadx
in the condition.remove
x--;
from your code.
and then your JQuery code must be like this:
$(document).ready(function() {
var max_fields = 30; //maximum input boxes allowed
var wrapper = $(".addmore_box_date"); //Fields wrapper
var add_button = $("#addmoredate"); //Add button ID
$('#add_date').datetimepicker({
timepicker:false,
format:'Y-m-d',
formatDate:'Y/m/d',
minDate:'-1970/01/02', // yesterday is minimum date
maxDate:'+2017/12/01', // and tommorow is maximum date calendar
});
var x = 1; //initlal text box count
var count = $('.add_button').length;
$(add_button).click(function(e) { //on add input button click
e.preventDefault();
if(count < max_fields){ //max input box allowed
x++; //text box increment
var newRow = $("<span id='date_time_close'><div class='row'><div class='col-xs-6 col-sm-4 col-md-4'><input type='text' name='add_date[]' class='form-control' id='add_date"+x+"' placeholder='Select date'></div><div class='col-xs-6 col-sm-4 col-md-4'><div class='new_select'></div><a class='remove_date_time pull-right'> Close</a></div></div></span>");
newRow.find('.new_select').append($('select.add_time').clone().attr('class', 'form-control add_time'+x));
$(wrapper).append(newRow);
$('#add_date'+x).datetimepicker({
timepicker:false,
format:'Y-m-d',
formatDate:'Y/m/d',
minDate:'-1970/01/02', // yesterday is minimum date
maxDate:'+2017/12/01', // and tommorow is maximum date calendar
});
}
});
$(wrapper).on("click",".remove_date_time", function(e){ //user click on remove text
e.preventDefault(); $('#date_time_close').remove();
})
});
Description:
when you remove the add_date
element on close button
click, and decrease the x variable, the datetimepicker object of the removed element is in the memory and not removed.
when you add a new element with the name of recently removed element, you can not define another datetimepicker object with the same name.
Post a Comment for "Jquery Clone() Issue"