Better understanding of Drupal behaviors I read about Drupal behaviors today, and I tried writing the following code.

(function ($) {

  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {
      $('#mymodule_id', context).change(function () {
        alert('Handler for .change() called.');
      });
    }
  };

}(jQuery));



Is Drupal.behaviors.mymodule the namespace?
What are the context and settings parameters passed to the Drupal behavior?
Is this the equivalent of document.ready()?  
Can I attach any number of functions? 
Can I define JavaScript functions which will be called somewhere?
In short, the advantage of Behaviors over document.ready() is that they are automatically re-applied to any content loaded through AJAX. mymodule is your namespace, which needs to be unique. context is the part of the page for which this applies, for example a part of a form that has been updated with AJAX. You can attach multiple behaviors, but I think you need to use a unique name (mymodule) for each of those.

Have a look at the following resources for more information:


Drupal behaviors
Drupal Javascript API
Changes in the Drupal JS API for D7