<div id="formheadtop">
<input class="checkbox" type="checkbox" /></div><div class="formbody"></div>
<div id="formheadtop">
<input class="checkbox" type="checkbox" /></div><div class="formbody"></div>
<div id="formheadtop"><input class="checkbox" type="checkbox" /></div><div class="formbody"></div>
$(function() { $('input:checkbox').live('click', function () {
if ( $(this).attr('checked') == true )
{
$(this).nextAll('.formbody:first').fadeIn();
}
else
{
$('.formbody').fadeOut();
};
});
The Code doesn’t work. I want to fade out only the next div.formbody.
First we’ll need to change
id="formheadtop"toclass="formheadtop", sinceIDs must be unique. Then you can use this code to fade in and out theDIVs:jQuery
You could shorten
$(this).parent().next('.formbody')to$(this).parent().next().fadeIn(), but I put in the selector, just in case you ever want to put something in between.HTML
I made the checkboxes checked by default. If not, you’ll need to hide the content in the
formbodytags.Here’s the code in action.