Code Star Code Star - 2 years ago 130
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.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download