Populating array of input fields as alternate fields using jQuery UI datepicker

Posted by Micor on Stack Overflow See other posts from Stack Overflow or by Micor
Published on 2010-04-08T04:20:32Z Indexed on 2010/04/08 4:23 UTC
Read the original article Hit count: 611

Filed under:
|

I am using jquery ui datepicker in order to populate start and end dates for multiple events on the same page, the number of events is dynamic and I need to post day, month and year as separate fields, so I have something like this:

<input type="text" name="event[0].startDate" class="date"/>
<input type="hidden" name="event[0].startDate_day" class="startDate_day" />
<input type="hidden" name="event[0].startDate_month" class="startDate_month" />
<input type="hidden" name="event[0].startDate_year" class="startDate_year" />

<input type="text" name="event[0].endDate" class="date"/>
<input type="hidden" name="event[0].endDate_day" class="endDate_day" />
<input type="hidden" name="event[0].endDate_month" class="endDate_month" />
<input type="hidden" name="event[0].endDate_year" class="startDate_year" />

event[1]...
event[2]...

I started working on jQuery functionality and this is what I have so far, which will populate alternate fields for startDate by class, of course it will not do what I need because I need to populate by field name rather then class:

$(".date").datepicker({
    onClose: function(dateText,picker) {
        $('.startDate_month').val( dateText.split(/\//)[0] );
        $('.startDate_day').val( dateText.split(/\//)[1] );
        $('.startDate_year').val( dateText.split(/\//)[2] );
}});

I need help figuring out how can I get the name of the input field within datepicker function so the alternate field assignment done by that field name + _day, month, year so this function can work for all the events on the page, making function above look more like:

$(".date").datepicker({
    onClose: function(dateText,picker) {
        $('input[' + $name + '_month' + ']').val( dateText.split(/\//)[0] );
        $('input[' + $name + '_day' + ']').val( dateText.split(/\//)[1] );
        $('input[' + $name + '_year' + ']').val( dateText.split(/\//)[2] );
}});

Hope that makes sense :) Thanks

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ui