Friday, 18 June 2010

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.

  1. Drupal.behaviors.DateChange = function (context) {
  2.  
  3. $('#<field_name>_values tr fieldset').each(function() {
  4. var fieldset = $(this);
  5. var fromDateField = fieldset.children('div').eq(0).children('div').eq(0).children('div').eq(0).children('input.form-text');
  6. var toDateField = fieldset.children('div').eq(1).children('div').eq(0).children('div').eq(0).children('input.form-text');
  7. toDateField.focus(function() {
  8. var fromDateVal = fromDateField.val();
  9. if (fromDateVal) {
  10. toDateField.val(fromDateVal);
  11. }
  12. });
  13.  
  14. var fromTimeField = fieldset.children('div').eq(0).children('div').eq(0).children('div').eq(1).find('input.form-text');
  15. var toTimeField = fieldset.children('div').eq(1).children('div').eq(0).children('div').eq(1).find('input.form-text');
  16. toTimeField.focus(function() {
  17. var fromTimeVal = fromTimeField.val();
  18. if (fromTimeVal) {
  19. toTimeField.val(fromTimeVal);
  20. }
  21. });
  22. });
  23. };

Written byBruno De Bondt

While trained to be a journalist and developing sites for fun, his experience as a webmaster for an ngo and years of volunteering for Indymedia.be led to his current site and server management expertise. This Drupal Developer/Themer & Linux Administrator is the Krimson link between web development & communication. The following quote sums up pretty well why he does what he does: 'Whatever your first issue of concern, media had better be your second, because without change in the media, progress in your primary area is far less likely.'

Thanks for the share Bruno!

Thanks for the share Bruno!

Thanks of course for the tip.

Thanks of course for the tip. I'm commenting principally to say how good your site looks. I can't think of too many drupal blog-portfolios as well-designed as this. Great work!

Thanks Nigel

Thanks, we are proud of it too :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h4> <br>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.