Drupal 7: Drupal.behaviors isn't running

594 views Asked by At

I've got a custom module I'm creating called touchpoints. At the top of the touchpoints.module file I have the following:

global $base_path;

$my_settings = array(
  'basepath' => $base_path,
  'module_path' => drupal_get_path('module','touchpoints')
 );

drupal_add_js(array('touchpoints' => $my_settings), 'setting');
drupal_add_js(drupal_get_path('module','touchpoints') . '/touchpoints.js');

Then in my touchpoints.js file I have the following:

Drupal.behaviors.touchpoints = function(context){
    $('.form-item-touchpointlocation').css('display','block');
    $('.form-item-touchpointcategory').css('display','none');
}

It's my understanding that anything inside the Drupal.behaviors call should run when the DOM finishes loading similar to a $(document).ready call. However, this code isn't being executed. Just to be sure I put an alert in the function as well, and it wasn't being triggered. Is there a step I'm missing?

1

There are 1 answers

1
LoneWolfPR On BEST ANSWER

I realized I was using Drupal 6 syntax. In Drupal 7 you have to declare behaviors differently. It should have looked like this

(function ($) {
    Drupal.behaviors.touchpoints = {
        attach: function (context, settings) {
          $('.form-item-touchpointlocation').css('display','block');
          $('.form-item-touchpointcategory').css('display','none');
        }
    };
})(jQuery);

For reference see: http://drupal.org/node/756722