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

Hi I want to add a click event to an element that hides another element. I have added a script using my theme's info file, the contents follow:

(function ($) {

  Drupal.behaviors.showhide = {
    attach: function (context, settings) {
      $('p.seekerclick').click(function(){
        $('div#block-views-exp-aid-search-for-seeker-page').hide();
      });
    }
  };

})(jQuery);

I can verify with firebug that that script is being loaded, but the element does not show a click event and clicking on it does not hide the other element. What is wrong?

The click element is in a custom block and the hide element is in a view exposed as block. Is there a setting in Drupal to activate javascript? Is there a better place to add the script instead of from the theme info file? When I look at the page with firebug, should I see the "onclick" attribute added to the click element?

share|improve this question
Looks like a bracket issue with that selector if you have pasted it correctly. – Alfred Armstrong Apr 3 at 17:27
1  
$('p.seekerclick'.click should be $('p.seekerclick').click – Alfred Armstrong Apr 3 at 17:33
Thanks, I fixed this and still not working. – Jim Cory Apr 4 at 14:09
You won't see an onclick attribute. – Alfred Armstrong Apr 4 at 14:14
You should be able to debug this yourself using Firebug. Here's some useful tips. thecodecentral.com/2007/08/01/debug-javascript-with-firebug – Alfred Armstrong Apr 4 at 14:17
show 1 more comment

3 Answers

take the .click out of the equation. should be ('p.seekerclick').click

share|improve this answer
Thanks for the suggestion. I made the change but still not working. Is there a problem if the click element is in a custom block and the hide element is in a view exposed as block? – Jim Cory Apr 3 at 20:32

There is a bug in your code (the click() event handler needs to be attached on the results of the $ selector, not be part of the selector).

This is your code (incorrect):

$('p.seekerclick'.click(function(){
  $('div#block-views-exp-aid-search-for-seeker-page').hide();
});

This would be the correct implementation:

$('p.seekerclick').click(function() {
  $('div#block-views-exp-aid-search-for-seeker-page').hide();
});
share|improve this answer
This was fixed, but still not working. – Jim Cory Apr 4 at 14:08

Not too sure, but I put my javascript code between the Drupal js. perhaps it will help you?

edit: Code changed, pls try again.

   (function($)  {
      function nameofyourmodule_function(context)  {

      // from here

   $('#somediv').click(function(e) {  
    $("#hidethisdiv").hide()
     });

    //till here
      }

      Drupal.behaviors.custommodule = {
        attach: function(context)  {
          nameofyourmodule_function(context);
        }
      }
    })(jQuery);
share|improve this answer
Thanks for responding. Not sure what you meant by "between the Drupal js", but I rewrote the script as you indicated and still not having desired effect. – Jim Cory Apr 4 at 14:08
pls try again with supplied javascript – user1973842 Apr 4 at 17:39

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.