Q:

Jquery body click stops toggle

I have a div that needs to animate up/down on the click of a button, and, if it has been animated down (or opened) it must close if the body is clicked. This works fine however if I use the body to close it the toggle button doesn't work again on the first click. I'm hoping there is a simple fix to this, any help is greatly appreciated, thanks!
$('#button').toggle(function(event){
        event.preventDefault();
        $('#box').animate({top: '0'}, 300);
    },function(event){  
        event.preventDefault();    
        $('#box').animate({top: '-100px'}, 300);
    });

    $("body").click(function(event) {
        if ($(event.target).closest('#box').length === 0) {
            $('#box').animate({top: '-100px'}, 300);
        }
    });

http://jsfiddle.net/user1688604/ve7VA/1/

A:

toggle event method is deprecated, you can use click method instead. Using click and and a conditional statement can solve the issue.
$('#button').click(function (event) {
    event.preventDefault();
    var $box = $('#box'),
        val = $box.css('top') === '0px' ? '-100px' : '0px';
    $box.stop().animate({
        top: val
    }, 300);
});

http://jsfiddle.net/Wdjys/

