I am using jquery datetimepicker with the following version
JQuery v1.7.2
JQuery UI - v1.10.3
jQuery timepicker addon V1.3
In a module, I am creating input field dynamically by clicking "+" as well as binding the datetimepicker for those input which are created dynamically as well as removing that input field by clicking "-".
My dynamic input fields always create like below:
datepickerfrom_1_1
datepickerfrom_1_2
datepickerfrom_1_3
datepickerfrom_1_4
datepickerfrom_1_5
.
.
.
I am binding the datetimepicker by using jQuery like below:
$(window).load(function() {
$('body').on('focus', 'input[id^="datepickerfrom"]', function() { // Id's Containing "datepickerfrom" string, bind the datetimepicker
$(this).datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'HH:mm:ss'
});
});
});
The script(datetimepicker)
is working fine when dynamic input is created.
Issue: I am adding some dynamic input field by clicking "+", then I removed some of the inputs by clicking "-", again I am adding some dynamic input fields. The second time created input field not calling/binding the datetimepicker properly.
What might be the issue? How can I solve it?
I just tried it in jsfiddle and it worked, here is the link: http://jsfiddle.net/juTRR/
If you know your problem, reproduce it in jsfiddle so we can see what it is, because I don't see it. Maybe try to use $(document).ready
instead of $(window).load
, its the only thing I changed. Or maybe the problem is actually in your adding and removing functions, if so you should put them in your post.
I think the problem is the jquery context in the 'on' function. In the function jquery.on you use $(this). $(this) will refer to you primary selector: $('body').
Have you tried:
$('input[id^="datepickerfrom"]').on('focus', function() {
Examples shows you need an input field. So I assume $('input[id^="datepickerfrom"]') is your input field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With