Maarten Hartman
Maarten Hartman

Reputation: 1629

drupal: javascript not being attached after ajax call (drupal attach behavior)

I'm loading a custom block into a div using AJAX, this works well, now the problem is: the javascript (jquery) is not being attached to the block contents. It does work when I load the block the regular way. I know this has something to do with drupal attach behavior, but I'm doing it wrong. I've wrapped the jquery I want to use like this:

    (function($)  {

    $(document).ready(function(){

    // the code goes here

    });

    // code to make it work with drupal    
    jQuery('.ajax-processed').once().ajaxSuccess(function() {
      Drupal.attachBehaviors();
    });

})(jQuery); 

Not sure where it's going wrong. Please tell me if you need more info in order to help me.

Thanks in advance

Upvotes: 3

Views: 4285

Answers (1)

Maarten Hartman
Maarten Hartman

Reputation: 1629

Found the solution in the meanwhile, had to wrap the code in this:

( function ($) {
    Drupal.behaviors.mymodule = {
        attach: function(context,settings) {

//code goes here

        }
  };
})(jQuery);

Upvotes: 3

Related Questions