I'm trying to add some custom jquery code to my theme. I used the code exemple below :

(function($) {
Drupal.behaviors.myBehavior = {
  attach: function (context, settings) {

    //code starts
    $("body").click(function() {
      alert("Hello World");
    //code ends


The jquery file is loaded with the script but nothing happens on my page, any help ?

  • 1
    The jquery file is loaded with the script what exactly do you mean by that? Is your custom JS file included in the page source? If so, your code will do exactly what it says it will, if it doesn't you either have other JS errors on the page stopping it from running, or your browser is broken. If you haven't added the JS file to the page, you need to add the file to the page. drupal_add_js() or adding to the theme .info file are the standard methods – Clive Aug 1 '13 at 13:45
  • Clive, I mean the script is included in the page but not working. – user19709 Aug 1 '13 at 13:47
  • 2
    You must have other JS error in the console stopping your code from running then - there's no good reason, from a Drupal point of view at least, why your code wouldn't run unless there are outside influences. Take out the event assignment and put the alert() directly in the attach function, that will tell you if your script is loading correctly. If that works, you just have a problem with bubbling events up to the body, or something like that – Clive Aug 1 '13 at 13:48
  • 1
    Thanks Clive, in fact, I had other JS errors in the console, I left out the scripts and the code is working correctly. Thanks a lot – user19709 Aug 1 '13 at 13:54
  • 1
    What he says ;) Hint: Use Google Chrome or Firefox with Firebug. Don't know if you are doing this already or not, but, well. Edit: seems you knew it. Consider posting self-answer now when you know the reason. – Mołot Aug 1 '13 at 13:54

Your Answer

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

Browse other questions tagged or ask your own question.