1

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?

4
  • I cannot duplicate this problem, see this jsFiddle. - Please consider making something similar so we can see the scenario involved. Commented Aug 29, 2012 at 14:57
  • Ohgodywhy can you please write an answer so that I can give you credit for this. Helped me spot my error. Commented Aug 29, 2012 at 15:48
  • 1
    It turns out my Laravel code was adding an id to each input id="date_from[]". When I introduced this to your JSFiddle code it behaved the way I explained. I have since rebuilt the ID of each element in JS as I construct it to accommodate that. And it works Commented Aug 29, 2012 at 16:12
  • One other thing. If I add another row after the page has loaded datepicker will not pick this up. Is that correct? Should I reinitiate datepicker then? Commented Aug 29, 2012 at 16:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.