Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I've got two different modules which in many ways work together. One of the things they work together on is that module A loads content via AJAX, and module B needs to attach event listeners to the content loaded.

However, it appears that when content is loaded via AJAX by module A, only the 'attach' function of Drupal.behaviours.a gets invoked, and not the 'attach' function of Drupal.behaviours.b. Both functions gets invoked on page load, but not on subsequent AJAX content loading.

// This runs every time module A loads new content.
Drupal.behaviors.a = {
  attach: function(context, settings) {
    alert('a:attach');
  }
}

// This only runs on page load, but never when module A loads new content.
Drupal.behaviors.b = {
  attach: function(context, settings) {
    alert('b:attach');
  }
}    

How do i make the 'attach' function of Drupal.behaviours.b run every time any module loads new content?

share|improve this question
    
Have you tried adding Drupal.attachBehaviors(); to the ajax success callback? –  Clive Sep 9 '12 at 12:36
    
No, but should that not be taken care of automatically? I thought that was the whole idea of the 'attach' function; that it should be called on page load and page update? It does seem, however, that only the 'attach' function for the invoking module gets called on page update though? –  sbrattla Sep 9 '12 at 13:27
    
That was my understanding yes, I haven't ever looked into it too deeply though. I know there's been times when after an AJAX call I've needed to reattach behaviours so I just assumed it was required –  Clive Sep 9 '12 at 13:31
    
Looking at drupal.js : "Drupal.attachBehaviors is added below to the jQuery ready event and so runs on initial page load. Developers implementing AHAH/Ajax in their solutions should also call this function after new page content has been loaded, feeding in an element to be processed, in order to attach all behaviors to the new content." It does seem that I need to invoke the attachBehaviours myself. But; it puzzles me that module A's 'attach' function runs even though I haven't invoked attachBehaviours in the AJAX success callback...? Why just for the invoking module, and not all modules? –  sbrattla Sep 9 '12 at 13:33
    
That puzzled me too a bit, it's hard to say without seeing the code in that module's attach function though –  Clive Sep 9 '12 at 13:38
show 3 more comments

2 Answers

This, from the comments in misc/drupal.js, explains how it's supposed to work:

  • Drupal.attachBehaviors is added below to the jQuery ready event and so
  • runs on initial page load. Developers implementing AHAH/Ajax in their
  • solutions should also call this function after new page content has been
  • loaded, feeding in an element to be processed, in order to attach all
  • behaviors to the new content.
share|improve this answer
add comment

Try adding this in Drupal.behavior.b:

Drupal.attachBehaviors($(this));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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