0

I nested jQuery code inside a behavior. I don't understand, why the code does not execute. I would be appleased if you could give me a hint on what I probably did wrong.

My code:

$(function (){
    Drupal.behaviors.block_sidebar_register_buttons = {
        attach: function (context, settings) {
            var element = $('#block_register_buttons');
            //...
        }
    };
});
3

Drupal disables the $ namespace for jQuery, to avoid potential conflicts with other libraries. Therefore the wrapping $() DOM-ready-function will neither be executed, nor can the $('#something') selector be used inside. Wrap your behavior with this instead:

(function ($, Drupal) {
  //inside the wrapper you can use the usual jQuery $ notation
  Drupal.behaviors.block_sidebar_register_buttons = {
    attach: function (context, settings) {
      var element = $('#block_register_buttons');
      //...
   }
  }
}(jQuery, Drupal));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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