0

If you go to https://www.drupal.org/node/756722#behaviors at Behaviors section you will see:

  (function ($) {
     Drupal.behaviors.exampleModule = {
     attach: function (context, settings) {
     $('.example', context).click(function () {
     $(this).next('ul').toggle('show');
  });
 }

};

})(jQuery);

I think 'example' should be the name of a class like:

   <p class="example"><?php echo click here?> <?p>

so when clicking on "Click here", the toggle will work. Am I right? And also what is 'ul' in $(this).next('ul').toggle('show');?

So if I have :

 for($b=1;$b<$number;$b++)
   {  ?>  
      <p class="toggler" style="cursor:pointer;"><?php echo "Commentsfor Paragraph $b:" ."<br>" . "<br>" ; ?> </p> 
     <div class="comment"> 
    <?php
      print render($p[$b]); 
      ?>
     </div> <?php  

      } 

and what I want to hide or show is the in , then can I write:

      <script type="text/javascript">
       (function ($) {
         Drupal.behaviors.exampleModule = {
        attach: function (context, settings) {
         $('.toggler', context).click(function () {
          $(this).next('div').toggle('show');
        });
      }
     };


   })(jQuery);
 </script>

or can I mention the class or id of div in $(this).next('div').toggle('show'); , so that the data in that part will hide or show? I hope I am clear.

  • I have updated my answer with a little bit extra, however I won't go into more detail about the solution to your problem because this question was originally about the behavior example. Keep your other question to get more into the specifics of your custom code. – rooby Nov 10 '14 at 2:58
2

Those examples are not complete working code examples.

They are just to illustrate that in Drupal 6 you did this:

Drupal.behaviors.exampleModule = function (context) {
  // Your code goes here.
}

And in Drupal 7 you would generally do this:

Drupal.behaviors.exampleModule = {
  attach: function (context, settings) {
    // Your code goes here.
  }
};

The rest of the code in those examples is not relevant.

However:

  1. Yes, it means that if you click an element with the class "example" it will run the code in your click handler. Your example code is buggy though, it should be like this: <p class="example"><?php echo 'click here'; ?> </p>

  2. The "ul" is referring to an unordered list element. So it will get the next ul from the element you clicked and toggle it.

[EDIT]

To answer your additional question about the example code:

or can I mention the class or id of div in $(this).next('div').toggle('show'); , so that the data in that part will hide or show?

Yes you can specify any selector you want, for example:

$('.comment').toggle('show'); 
  • so if I have something like: for($b=1;$b<$number;$b++) { ?> <p class="toggler" style="cursor:pointer;"><?php echo "Commentsfor Paragraph $b:" ."<br>" . "<br>" ; ?> </p> <div class="comment"> <?php print render($p[$b]); ?> </div> <?php } since what I want to toggel is the part in <div></div> can I say <script type="text/javascript"> (function ($) { Drupal.behaviors.exampleModule = { attach: function (context, settings) { $('.toggler', context).click(function () { $(this).next('div').toggle('show'); }); } }; })(jQuery); </script>? – user3530082 Nov 10 '14 at 2:42
  • Please post code in your original question. It's too hard to read in the comment. – rooby Nov 10 '14 at 2:43
  • I have also asked a question at drupal.stackexchange.com/questions/136285/… – user3530082 Nov 10 '14 at 2:43
  • I updated my question.@rooby – user3530082 Nov 10 '14 at 2:50
  • so how can I mention the id or class of that div? So that if I have a div inside another div, it works correctly. Because as I have mentioned in my other question that I have mentioned in above, it does not work correctly now. so I thought that if instead of saying div in $(this).next('div').toggle('show');, I could put the id or class of div ,it works correctly. Can you also please look at my other question and see if you can help me. I would really appreciate it. @ rooby – user3530082 Nov 10 '14 at 3:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.