Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created November 26, 2010 21:07
Show Gist options
  • Save gavinblair/717214 to your computer and use it in GitHub Desktop.
Save gavinblair/717214 to your computer and use it in GitHub Desktop.
$('fieldset.collapsible:not(.tao-processed) > legend > .fieldset-title').each(function() {
var fieldset = $(this).parents('fieldset').eq(0);
fieldset.addClass('tao-processed');
// Expand any fieldsets containing errors.
if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
$(fieldset).removeClass('collapsed');
}
// Add a click handler for toggling fieldset state.
$(this).click(function() {
if (fieldset.is('.collapsed')) {
$(fieldset).removeClass('collapsed').children('.fieldset-content').show();
}
else {
$(fieldset).addClass('collapsed').children('.fieldset-content').hide();
}
return false;
});
});
@SeanJA
Copy link

SeanJA commented Nov 27, 2010

What the what now?

also, you can switch this $(this).click(function() { if else } with $(this).toggle(function() { }, function(){} ) which is way awesomer (because you can have as many as you want...)

@gavinblair
Copy link
Author

This is actually copied from Drupal's collapsible fieldset functionality. It has to be run again on dynamically added fieldsets.

@SeanJA
Copy link

SeanJA commented Dec 2, 2010

$('fieldset.collapsible').bind('click', function(){
    $(this).toggleClass('collapsed'); // where collapsed is display:none or height = 10px, overflow: hidden (or whatever hides the fieldset)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment