Filling in the 'To'-date fields with the 'From'-date values using jQuery
Many thanks to Bruno De Bondt for posting this snippet at krimson.be!
When creating nodes in Drupal that have one or more date fields, it can be handy if the values for the 'To' date fields (date & time) are automatically changed to the values that were filled in the 'From' date fields. For example, when events take place on the same day, this can ease data input and make life easier for authors.
The following code does this by using a bit of jQuery. This code is pretty generic, for use with date.module (http://drupal.org/project/date, v6.x-2.4, the stable release at the time of writing).
It should be pretty straight forward to adapt this for use in your own project. The only thing that needs to be changed is the < field_name > in the first selector's ID: you should replace this with your date field's name, since date.module doesn't provide us with a more generic ID.
Drupal.behaviors.DateChange = function (context) {
$('#<field_name>_values tr fieldset').each(function() {
var fieldset = $(this);
var fromDateField = fieldset.children('div').eq(0).children('div').eq(0).children('div').eq(0).children('input.form-text');
var toDateField = fieldset.children('div').eq(1).children('div').eq(0).children('div').eq(0).children('input.form-text');
toDateField.focus(function() {
var fromDateVal = fromDateField.val();
if (fromDateVal) {
toDateField.val(fromDateVal);
}
});
var fromTimeField = fieldset.children('div').eq(0).children('div').eq(0).children('div').eq(1).find('input.form-text');
var toTimeField = fieldset.children('div').eq(1).children('div').eq(0).children('div').eq(1).find('input.form-text');
toTimeField.focus(function() {
var fromTimeVal = fromTimeField.val();
if (fromTimeVal) {
toTimeField.val(fromTimeVal);
}
});
});
};