Skip to content

Instantly share code, notes, and snippets.

@martinwolf
Last active December 23, 2015 19:49
Show Gist options
  • Save martinwolf/6685663 to your computer and use it in GitHub Desktop.
Save martinwolf/6685663 to your computer and use it in GitHub Desktop.
Click Toggle Thingy
/* General Click Toggle */
$('[data-click-ref]').on('click', function(ev) {
var clickRef = $( $(this).data('click-ref') );
if ( clickRef.data('clicked-from') ) {
// If clickRef was already opened once
var clickedFrom = clickRef.data('clicked-from');
if ( clickedFrom === $(this) ) {
// If clickedFrom is the same link as the one that's clicked right now
$(this).toggleClass('active');
} else {
// Else toggle active on the link that originally was clicked
clickedFrom.toggleClass('active');
}
} else {
// Toggle active on first click and add original clicked link to clickRef
$(this).toggleClass('active');
clickRef.data('clicked-from', $(this));
}
// Toggle the clickRef
clickRef.toggleClass('active');
ev.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment