I have a #clickme button. When the div #cart clicked, the slide show, when it is clicked again, it slides down.
I also want to be able to close it by clicking elsewhere on the page other than #cart or #clickme.
The code I tried does not quite work. Now it happens that when I click #clickme, it slides, and then quickly goes back.
I assume that the click event is triggered both at the document level and at the #clickme level, causing one effect after another.
What an elegant way to get around this?
$('#clickme').click(function() {
if ($("#cart").is(":hidden")) {
$("#cart").slideDown("slow");
} else {
$("#cart").slideUp("slow");
}
});
$(document.body).click(function () {
if ($("#cart").not(":hidden")) {
$("#cart").slideUp("slow");
}
});
source
share