tags:

views:

660

answers:

3

I am attempting to open/collapse sections of my site that are fieldsets with the click event on the legend tag. However I'm going to need to use a wrapInner to add a div inside the fieldset to hide the content...however this also hides the legend (which I definately dont want to do) :-). How can I use wrapInner but specify not to hide the legend (or alternatively the first element contained within the fieldset - as it will always be the legend).

$("#mainarea fieldset").wrapInner("<div class='fieldsetWrapper'></div>");

$("#mainarea fieldset:not(:first)").addClass("fsClosed"); // Close all fieldsets within the main area (but not the first one)

$("#mainarea fieldset legend").mousedown(function(){  // When clicking the legend of a fieldset ...
 $("#mainarea fieldset:not(.fsClosed)").addClass("fsClosed");  // If it's already open, close it
 $(this).parent().removeClass("fsClosed");  // If it's closed, remove the closed class from the containing fieldset
 return false;
});

Cheers Mark

+2  A: 
$('#mainarea fieldset').children(':gt(0)').wrapAll("<div class='fieldsetWrapper'></div>");

This should do the trick.
Info on the wrapAll function: http://docs.jquery.com/Manipulation/wrapAll#html>

Edit
possibly even better:

$('#mainarea fieldset').children().not('legend').wrapAll("<div class='fieldsetWrapper'></div>");
Pim Jager
Perfect. Thank you. :)
Actually this appears to grab all content from ALL fieldsets on the page and place them in one DIV wrapper, rather than each fieldset having its own self contained DIV. Any ideas?
+2  A: 

In response to your comments in Pim's example, you need to loop through the fieldsets

$('#mainarea fieldset').each(function(){
   $(this).children().not('legend').wrapAll("<div class='fieldsetWrapper'></div>");
});

You could probably refactor that to something like this;

$('#mainarea fieldset').each(function(){
   $(':not(legend)', this).wrapAll("<div class='fieldsetWrapper'></div>");
});
Simon
A: 
$(document).ready(function(){
$("fieldset legend")
    .click(function(){
         $(this).parent().children().not('legend').toggle("slow");
         });
});