OK let's start with the code:
CSS:HTML Code:<div id="content"> <div class="openbox"> <h2>Intro</h2> <div> <p>Here is an openbox, it should open/close nicely </p> </div> </div> <div class="openbox"> <h2>Intro</h2> <div> <ul> <li>Football </li> <li>Tennis </li> <li>Rugby </li> </ul> </div> </div> </div>
Code:.openbox{ cursor:pointer; width:200px; } .open { background-color: green; } .closed { background-color: red; } .closed div{ display:none; }
Javascript:
The above code works like a charm, but I would like to use the effects available in jQuery to animate the opening/closing. Is it a case of using the animate() function? I understand the function but how do I select the "div" within the "this" selector. If I do:HTML Code:<script type="text/javascript"> $(".openbox").addClass("open"); $(".openbox h2").click(function() { var openbox = this.parentNode; if($(openbox).hasClass("open")) { $(openbox).removeClass("open"); $(openbox).addClass("closed"); } else { $(openbox).removeClass("closed"); $(openbox).addClass("open"); } }); </script>then it applies the closing to both divs. I just want one of the divs to collapse, depending of which h2 I clicked on.Code:$(".openbox div").animate({height: 0}, 600)
Edit: the "height" attribute won't actually succeed in properly hiding the div. Can one use something like "display: none"?


Reply With Quote
Bookmarks