- English
- Français
I thought this would be an easy one. Not so. What works in Drupal 6 doesn't necessarrily work in Drupal 7.
Let's take a simple example. How about a script that allows you to show/hide one or more paragraphs. I did a quick Google search and found numerous examples. Here's the one I picked & slightly modified for the sake of clarity.
<script type="text/javascript">
$(document).ready(function(){
$(".toggler").click(function(){
$(this).next().slideToggle("slow");
return false;
}).next().hide();
});
</script>
<p class="toggler" style="cursor:pointer;">Show/Hide the following paragraph(s).</p>
<div>
This paragraphe is initially hidden. Clicking on the link above will reveal (Show) all the content inside the DIV that immediately follows a "toggler" class paragraph.
</div>
In Drupal 6, simply paste this code in the Body field of a Page or Article node and it works. Well, as usual, there are a few requirements. For the most part, this should do it.
I tried this exact same method in Drupal 7 but by default the <script></script> tags and its content are removed (deleted) by the system for security reasons. There are probably ways to bypass this but it's bad practice.
So we can't embed this script in a Drupal 7 Article node. In the present case, the next best way - a proper way - is to embed the script directly into the Article template of the theme. In doing so, the script will be available for all nodes created with the Article content type. Now, that's even better. So how do we do this?
Click here to Show or Hide the next paragraph!
<script type="text/javascript">
(function($) {
// Javascript code
}) (jQuery);
</script>In the end, here's what the node--article.tpl.php should roughly look like.
...
* @see template_preprocess()
* @see template_preprocess_node()
* @see template_process()
*/
?>
<script type="text/javascript">
(function($) {
$(document).ready(function(){
$(".toggler").click(function(){
$(this).next().slideToggle("slow");
return false;
}).next().hide();
});
})(jQuery);
</script>
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
...
Ready for some magic?
<style type="text/css">
p.toggler:hover {
color:#0071B3;
}
</style>
<p class="toggler" style="cursor:pointer;">Show/Hide the following paragraph(s).</p>
<div>
This paragraphe is initially hidden. Clicking on the link above will reveal (Show) all the content inside the DIV that immediately follows a "toggler" class paragraph.
</div>
Save and then Show/Hide to your heart's content!
Comments
Alex Weber
Mon, 02/20/2012 - 00:32
Permalink
Drupal Behaviors
admin
Mon, 02/20/2012 - 07:34
Permalink
Care to expand?
Hi Alex. It would be great if you could expand on how you would use Behaviors in the current example.
mongolito404
Mon, 02/20/2012 - 08:17
Permalink
Drupal Behaviors
Using Drupal behaviors and jQuery once, the code would be
(function($) { Drupal.behaviors.customToggler = { attach: function(context, settings){ $(".toggler", context).once('custom-toggler').click(function(){ $(this).next().slideToggle("slow"); return false; }).next().hide(); }; }; })(jQuery);This allows the script to work for elements added after the page loads.
Anonymous
Mon, 02/20/2012 - 04:28
Permalink
I also suggest to remove the
<a href=""> ... </a>) leaving the clickable text inside the "p" and adding a line of CSS with the "cursor:pointer" property.admin
Mon, 02/20/2012 - 08:40
Permalink
Good point
Done. Thanks for the suggestion.
Alex Weber
Mon, 02/20/2012 - 09:35
Permalink
Awesome! :)
Alex Weber
Mon, 02/20/2012 - 09:38
Permalink
Using behaviors in your example
It would look something like this:
(function($) { Drupal.behaviors.my_node_toggle = { attach: function(context, settings) { $(".toggler", context).click(function(){ $(this).next().slideToggle("slow"); return false; }).next().hide(); }); } }; })(jQuery);That said, I believe your javascript could be put in an external file and added using a preprocess funciton to keep the template free of non-markup! :) Sorry, don't mean to be annoying or a stickler! :)
lee
Mon, 02/20/2012 - 16:30
Permalink
likewise the css belongs in a
likewise the css belongs in a file instead of style tags, again being anal
also you might want to use jquery once or add a 'processed' class/filter to your jquery to prevent the listener being attached twice (eg on ajax events completing) - eg
$('.toggler:not(.processed)', context).addClass('processed')...Drupal.attachBehaviors gets called when ajax events complete too.
Tim
Mon, 02/20/2012 - 18:04
Permalink
Question
Janin
Fri, 05/18/2012 - 13:33
Permalink
javascript to work within the webform of drupal 7
Janin
Fri, 05/18/2012 - 13:39
Permalink
javascript to work within the webform of drupal 7
FakeBit
Thu, 09/20/2012 - 04:04
Permalink
just use css
Add new comment