Skip to content

Instantly share code, notes, and snippets.

@wesruv
Last active September 19, 2015 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesruv/3e65616d38828dd85ba4 to your computer and use it in GitHub Desktop.
Save wesruv/3e65616d38828dd85ba4 to your computer and use it in GitHub Desktop.
Generic Toggle JS for Drupal 7
<a href="#" class="js-dropdown__toggle">Toggle Link</a>
<div class="js-dropdown__wrapper">
<h3>Toggleable Content</h3>
<p>Can have other wrappers, but no tag should be between the toggle link and toggle wrapper.</p>
</div>
(function ($, Drupal, window, document, undefined) {
// Generic toggle behavior
Drupal.behaviors.toggleOpen = {
attach: function(context, settings) {
// Only run this stuff once
$('.js-dropdown__toggle', context).once('js-dropdown--triggered', function(){
$(this).click(function (e) {
e.preventDefault();
$(this).toggleClass('js-is-active');
$(this).next('.js-dropdown__wrapper').toggleClass('js-is-open');
$(this).blur();
});
});
}
};
})(jQuery, Drupal, this, this.document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment