Code Star Code Star - 5 months ago 46x
Javascript Question

jQuery datetimepicker not work on first time

I have a dynamic time field in which I used


For dynamic initialization I used this code:


But it works when I focus for second time.

A working demo would be great, if possible.


The issue is because you only instantiate the date picker on the element the first time it gets focussed, and then every time after, which isn't a good idea. Instead you only need to instantiate it on the element on load:

$(function() {

If the .time-input element is dynamically appended to the DOM (as I am assuming given the delegated event handler) then you need to call the datepicker() method on it at the point it's added to the DOM. For example:

    url: 'mypage.php',
    success: function(data) {
        if (data.prop) {
            var $input = $('<input class="time-input" />').appendTo('#myForm');