Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker - dynamic altfield issue

Tags:

jquery

I have multiple datepickers on a page... all seems to be working fine except I the altfield part...

Is something like this possible??

$(document).ready(function(){

    $( ".datepicker" ).datepicker({
        beforeShowDay: function(date){ return [(date.getDay() == 1 || date.getDay() == 4), ""] },
        showOn: "button",
        buttonText: "Book now...",
        showWeek: true,
        firstDay: 1,
        onSelect: function(date) { 
            $(this).parent().find('.selecteddate').prepend("Date: " + $(this).parent().find('.datepicker').val() + " - <a href='javascript:;' class='cleardate'>clear date</a>"); 
            $(this).parent().find('button').hide();
        },
        dateFormat: "DD, d MM, yy",

        altField: $(this).closest('div').find('.datepickeralt'),

        altFormat: "dd/mm/yy",
        navigationAsDateFormat: true,
        minDate: 0,
    });

    $(".cleardate").live('click',function(){
        $(this).closest('div').find('.datepicker').datepicker("setDate", null);
        $(this).closest('div').find('.datepickeralt').val("");
        $(this).closest('div').find('button').show();
        $(this).parent().html("");
    });

});
like image 920
Tom Avatar asked Sep 28 '10 10:09

Tom


1 Answers

Yes you can do exactly that, you just need to instantiate the date pickers with a .each() loop, like this:

$('.datepicker').each(function() {
  $(this).datepicker({
    altField: $(this).closest('div').find('.datepickeralt')
  });
});

This way inside the .each() this refers to what you want, each individual date picker, then finding its alt field. You can give it a try here.

like image 161
Nick Craver Avatar answered Oct 03 '22 03:10

Nick Craver