Stanton
Stanton

Reputation: 1374

How to set datetimepicker to value of the text box it's bound to

I'm using the Trent Richardson datetimepicker jQuery plugin bound to a class of HTML text box, but I can't figure out how to set the picker to match the boxes.

I'm invoking datetimepicker like this:

$('.c_datepick').datetimepicker({
    hourGrid: 12,
    minuteGrid: 30,
    timeFormat: 'hh:mm',
    showTimezone: false,    
})

The text boxes are populated with dates/times from my database. The only way I can think of to call the datetimepicker is to use the text box's click event, like this:

$('.c_datepick').click(function() {

    (Do something here to set the datetimepicker to the value of the text box)

    alert($(this).val());  // This alert shows the value I want

})

I put that alert box in the function to check that I can see the value I'd like to use, and it's there. My question is, what can I do to set the datetimepicker to the value in the box? Or is the click event even the place to do this?

I know that this can work, because when I use the datetimepicker to change the value of a text box, the datetimepicker remembers that value. In other words, once the datetimepicker has been 'made aware' of each box's contents, it does what I want.

There are some related questions on this site, but no answers. Can anyone help? Thanks.

Upvotes: 1

Views: 1790

Answers (2)

Stanton
Stanton

Reputation: 1374

I stumbled onto the solution. I was using strings in my text boxes. When I used Date and strToTime to convert them to dates, datetimepicker automatically recognized them.

Duh. Wasting everybody's time, sorry.....

Upvotes: 0

charlietfl
charlietfl

Reputation: 171679

If the plugin doesn't automatically parse the values itself you can initialize them within a loop and get value of each from within the loop:

$('.c_datepick').each(function () {    
  var $input = $(this);
  $input.datetimepicker({
    hourGrid: 12,
    minuteGrid: 30,
    timeFormat: 'hh:mm',
    showTimezone: false,
  }).datetimepicker('setDate',new Date( Date.parse($input.val())));
})

Assumes date value is a format that Date.parse will recognize

Upvotes: 1

Related Questions