Search code examples
jqueryjquery-uidatepickerjquery-ui-datepickerdatetimepicker

jQuery datetimepicker addon not working when attached to div rather than input element


I am using the jquery Trent Richardson datetimepicker addon which is working fine when linked to a text input (see link below).

http://jsfiddle.net/many_tentacles/xnn3M/1/

However, when I attach it to a div so that the picker shows all the time, and an altField option, the date and time do not display correctly in the altfield (see link below). I can create a similar problem if I add an altField to the version that works so I think it might have something to do with this.

http://jsfiddle.net/many_tentacles/EyPm5/


Solution

  • Hiya please see working demo here: http://jsfiddle.net/Dgnjn/

    D'uh don't forget to upvote and accept :))

    You can use the onSelect event:

    $('#datetimepicker').datetimepicker({
        //altField: '#alternate',
        dateFormat: 'yy-mm-dd',
        timeFormat: 'hh:mm:ss',
        stepHour: 2,
        stepMinute: 10,
        onSelect: function(dateText, inst) { 
          var dateAsString = dateText; //the first parameter of this function
          var dateAsObject = $(this).datepicker( 'getDate' ); //the getDate method
    
            $('#alternate').val(dateAsObject);
           // alert(dateAsObject + '==> ' + $('#alternate').val());
       }
    
    });
    

    The first parameter is in this case the selected Date as String. Use parseDate to convert it to a JS Date Object.

    See http://docs.jquery.com/UI/Datepicker for the full jQuery UI DatePicker reference.