1

This question already has an answer here:

I'm designing my first drupal theme and I'm not very well experienced in this kind of stuffs. I tried to add some jQuery to my design so I saved this code into a .js file and add it to my theme.info file:

$(document).ready( function() {
    $("button").click(function() {
        $("button").hide();
    };
};

It should work properly but it didn't. After some searching, find out for avoiding conflicts I should use drupal's function instead of $(document).ready so I did:

(function ($) {
            $("button").click(function(){
                $("button").hide();
    });
})(jQuery);

but it doesn't work out. And again, by googling I find out something named behaviors. Actually I can't understand what is it but after using examples as a pattern an overwriting my code into it, my issue fixed:

(function ($) {
    Drupal.behaviors.exampleModule = {
        attach: function (context, settings) {
            $("button").click(function(){
                $("#navigation").hide();
    });
        }
    }
})(jQuery);

So my question is: What is drupal behaviors?

I read some articles about it but couldn't understand it well.

Also some examples will appreciated.

marked as duplicate by Clive Mar 10 '15 at 8:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.