Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I try to include a simple script (below) and cant get it to work.

(function ($) {
  Drupal.behaviors.myModuleBehavior = function (context) {
    alert('say something pretty please');
  }
})(jQuery);

The script is added within a hook_form_alter function

drupal_add_js(drupal_get_path('module', 'my_module') . '/js/my_module_form.js', 'file');

Up to here, no issues. I see the script is added to the long line of js:extends with the correct path. Thus I would expect to receive an alert... but nothing happens.

As I've run out of things to check for and consulted docs, google and this site, only to find that the code should work, is anyone able to point out what might be wrong here?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 3 down vote accepted

I believe the = function (context) bit needs to include the attach:

(function ($) {
  Drupal.behaviors.myModule = {
    attach: function (context, settings) {
      alert('say something pretty please');
    }
  };
})(jQuery);
share|improve this answer
thank you - this works fine. – remote Jan 9 '12 at 22:37
add comment (requires an account with 50 reputation)

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.