Last active
December 23, 2015 19:49
-
-
Save martinwolf/6685663 to your computer and use it in GitHub Desktop.
Click Toggle Thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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