Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetimepicker not calling/binding properly after removing dynamic input field

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?

like image 640
DonOfDen Avatar asked Jun 14 '13 08:06

DonOfDen


2 Answers

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.

like image 121
Samuel Robert Avatar answered Oct 21 '22 15:10

Samuel Robert


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.

like image 1
jaapenstaart Avatar answered Oct 21 '22 16:10

jaapenstaart