rmccallum
rmccallum

Reputation: 214

jQuery Date Picker on Multiple Fields using array notation

Using JS to generate a list of input fields that have the name of:

name="date_from[]"

The class, 'date_picker', is instantiated using:

$( ".date_picker" ).datepicker({ dateFormat: "dd-mm-yy" });

When a new record is generated using JS the new input field does in deed display the Date Picker but the date selected is inserted into the first field, always.

Is there any solution, or maybe an option in the date_picker to make it insert into the currently selected field?

Upvotes: 1

Views: 1941

Answers (1)

Calvein
Calvein

Reputation: 2121

The datepicker has a onClose event wich provide a variable with the result. You need to take it, split it, and set it to your inputs :

var $input = $('input')
$input.datepicker({
    dateFormat: 'dd-mm-yy'
  , onClose: function(dateText) {
        for (var i = 0, date = dateText.split('-'), item; item = $input[i];) {
            item.value = date[i++];
        }
    }
})​

Here is a live example.

Upvotes: 1

Related Questions