I’m trying to make a header that can slide down to reveal a contact form and some other content, triggered by a div inside that header.
Here’s my HTML:
<div id="header">
<div id="content"></div>
<div id="button" class="openpanel"></div>
</div>
#header has a margin-top of -300px
And here’s my jQuery:
$(document).ready(function() {
$(".openpanel").click(function(){
$("#header").animate({
"margin-top": "0px"
})
$("#button").removeClass("openpanel")
$("#button").addClass("closepanel")
});
$(".closepanel").click(function(){
$("#header").animate({
"margin-top": "-300px"
})
$("#button").removeClass("closepanel")
$("#button").addClass("openpanel")
});
});
The problem is that it slides down perfectly, the classes show up properly, but when I click the button a second time the header doesn’t slide up again.
The issue with your code is that
.closepaneldidn’t exist when the click handler was bound. In such cases you delegate events using.onto the container/document object.See below,
Change
$(".openpanel").click(function(){as below,and
Change
$(".closepanel").click(function(){You can achieve what you are doing using a single function,