Hey,

I'm trying to add some behaviour to a couple of form elements using jQuery. I understand that this should be done
through Drupal.behaviours. However, regardless of what i do, things will simply not work!

The problem is that i can't find any details about this in the JavaScript API documentation for Drupal 7, so i've had
to look at a page describing how to do this for Drupal 6 (http://drupal.org/node/114774#javascript-behaviors).

This example should simply hide all div's on a page. I've tried all possible combinations of the code, but without success.
Nothing happens, regardless of what i do.

Drupal.behaviors.reportingBehaviour = function(context) {
  $('div', context).hide();
};

I'd really be happy if anyone has any suggestions as to how i can get things to work! And, for the record, all required
files have been included.

Comments

Heine’s picture

sbrattla’s picture

Thank's a lot. That solved it.

Would you, by the way, happen to know why i can't use $ inside the attach function? I need to explicitly write jQuery.

Drupal.behaviors.reportingBehaviour = {
  attach: function(context) {
    jQuery('div').hide();   
  }
};
Heine’s picture

carlwenrich’s picture

I can't get this to work either.

marcnischan’s picture

Working with a drupal 7 bartik child theme. I have my jQuery in the special "no-compete" wrapper - (function ($) { //normal jQuery code in here }(jQuery)); - to avoid conflict with other javaScript libraries. I've attached the js file in the .info page and it is listed in the head of every page. Also, I have added the behaviors code inside of the 'no-compete" wrapper - Drupal.behaviors.myThemeName = { attach: function(context, settings) { //here's my code }};

The slider that I built works great on my home page, but any code I write for other pages is completely ignored.

Admittely, I don't understand how behaviors works, but I figured as long as I wrapped my jQuery in it, it would work site-wide. So, it must have something to do with the last part of the permission: Drupal.behaviors.howMustThisBeNamedToWork

Any ideas?

paul.linney’s picture

You can put this in your template.php,

function THEME_preprocess_page(&$vars) {
drupal_add_js('(function ($) { //here's my code })(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 999));
}

Paul

Paul Linney
Owner & Web Developer