views:

68

answers:

1

i have a form in drupal with jquery based date module. there are multiple fields with date picker enabled. i want to set the value of all of them (they all have class .date-popup-init) to the value of the first field (#edit-field, the 'from' date) when that field is set. my code so far:

<script type="text/javascript">
        var DatePicked = function() {
                var firstdate = $("#edit-field");
                var updater = firstdate.datepicker("getDate");
                $(".date-popup-init").each(function(){
                  $(this).datepicker("setDate", updater);
                  });
        }
        $(function() {
                $("#edit-field").datepicker({
                        onSelect: DatePicked
                });
        });
</script>

this seems to randomly work; it sets the date of some fields to the value of #edit-field, seemingly different fields each time.

also, the form adds more datepicker-enabled fields via ajax. is there any way to ensure that all these new fields, when they load, pick up the value of #edit-field as well?

disclaimer: last night was my first attempt at javascript of any kind. i have a basic idea now. the above was cobbled through countless google examples.

A: 

You are on the right track. Try this:

var DatePicked = function(dateText) {
    $(".date-popup-init").val(dateText);
}
$('document').ready(function ()
{
    $(function() {
        $("#edit-field").datepicker({
            onSelect: DatePicked
        });
    });
});
nc3b
thanks. actually i just realized the problem with my code, as its the same with yours: on clicking #edit-field, other fields are set to the expected value only if i had clicked in them before i select a date in #edit-field. is like i have to 'activate' them first. and incidentally, any word on new fields added via ajax picking up the exiting value of #edit-fields?
Andy
@Andy: This did not happen to me. I don't have to focus any of the textboxes, it just works. When I choose a date, all the fields automatically get that date.
nc3b
@nc3b: been reading around. noted that all fields had been added to the page by an AHAH process. i suspect that may be part of the problem.
Andy