The structure of Drupal.behaviors is rather similar in Drupal 7 and Drupal 8. Where do attach() and detach() come from? Thid code is from drupal.js , still wondering where attach method in this.attach is defined?

Drupal.attachBehaviors = function (context, settings) {
  context = context || document;
  settings = settings || Drupal.settings;
  // Execute all of them.
  $.each(Drupal.behaviors, function () {
    if ($.isFunction(this.attach)) {
      this.attach(context, settings);

1 Answer 1


The attach() and detach() methods are part of the Behaviors interface defined by Drupal:

Any object defined as a property of Drupal.behaviors will get its attach() method called when the DOM has loaded both initially and after any AJAX calls.


  • Thanx. I see that I didnt have good understanding of JS and declaring methods, I was thinking this was some already declared method like api.jquery.com/append that I can use on elements, objects. I dig a bit deeper and saw what is what.
    – Blissful
    Jan 25, 2019 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.