Theming

Drupal.behaviors and Drupal-Specific Javascript for Drupal 6, 7

Last updated March 2, 2020
Categories

Check your version

This tutorial covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources

Sprout Video

Goes into depth about some of the JavaScript constructs and tools that Drupal provides when writing JavaScript for Drupal. Topics include using Drupal.behaviors and variable settings.

There where a couple of changes to the Drupal.behaviors system for D7. You can find out more information about the changes here: http://drupal.org/update/modules/6/7#drupal_behaviors

And more general information about using the new system here http://drupal.org/node/756722

Once you've converted from the old Drupal.behaviors.myModule = function(context) to the new syntax

Drupal.behaviors.myModule = {
attach: function(context, settings),
detach: function(context, settings)
}

The rest is pretty much the same.