I want to enhance some fieldsets with the option to show / hide their contents upon clicking their label.
Currently, the HTML looks like this:
<fieldset>
<legend>Fieldset 1</legend>
<!-- Some input, p, div, whatever -->
</fieldset>
<fieldset>
<legend>Fieldset 2</legend>
<!-- Some input, p, div, whatever -->
</fieldset>
So, on click of one fieldset legend
, anything but the clicked legend of the parent fieldset should be toggled.
I tried using this:
$("fieldset *:not(legend)").hide();
$("fieldset legend").click(function(){
$(this).nextAll().slideToggle();
});
But it doesn't do anything (not even hide the contents in the first place). Of course I want to only toggle the view on the very fieldset the user has clicked, so it has to somehow determine what legend was clicked and then hide the contents of the corresponding fieldsets.
Sure, I could give them all IDs and write the code for every fieldset, but thats rather redundant seeing it would always be the same I think there has to be a way to make this functionality universal for any amount of fieldsets...
Anyone has a neat idea?