Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 27, 2015 20:09
Show Gist options
  • Save martynchamberlin/7382770 to your computer and use it in GitHub Desktop.
Save martynchamberlin/7382770 to your computer and use it in GitHub Desktop.
Simple show/hide structure. I build this an absolute ton and need a place to reference it so I'm not constantly writing the same thing over and over again. The beauty of this structure is that you can have an unlimited number of .toggle elements on a page and there will not be any conflict. No unique IDs required.
<div class="toggle">
<a href="#" class="read-more">Read more</a>
<div class="hidden hiding" style="display:none">This text toggles back and forth</div>
</div>
jQuery(document).read(function( $ )
{
$('.read-more').click(function()
{
var hidden = $(this).closest('.toggle').find('.hidden');
if ( $(hidden).is('.hiding'))
{
$(hidden).removeClass('hiding').fadeIn('slow');
}
else
{
$(hidden).addClass('hiding').hide();
}
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment