Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Last active September 9, 2016 16:37
Show Gist options
  • Save Bobz-zg/0bb51a5b89cc2ce67503b19204d61417 to your computer and use it in GitHub Desktop.
Save Bobz-zg/0bb51a5b89cc2ce67503b19204d61417 to your computer and use it in GitHub Desktop.
Simple jquery toggle function
$.fn.toggler = function() {
return this.each( function() {
$(this).click(function(event) {
// Prevent click event on a tag
if ($(this).is('a')) {
if(event.preventDefault) { event.preventDefault(); }
}
$target = $(this).data('toggle')
$elem = $('#' + $target);
$height = $elem.prop('scrollHeight');
// Remove inline max-height
if (typeof $elem.attr('style') !== typeof undefined && $elem.attr('style') !== false) {
$elem.removeAttr('style');
}
else {
$elem.css('max-height', $height);
}
// Do something based on toggle target, eg scroll to position of element etc ..
switch ($target) {
case 'toggle-3':
$elem.toggleClass('active').css('background-color', 'red');
break;
default:
$elem.toggleClass('active');
break;
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment