views:

27

answers:

2

I have a jquery drop down that activates and deactivates when you click the header for the drop down

$j('#category_header').click(function() {
    $j('#category_dropdown').slideToggle("fast");
    return false;
});

but I want it to also close when I click anywhere on the page that isnt the drop down. How do I go about doing that?

+1  A: 

Look at the answer for this question: http://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element

SBUJOLD
this doesn't work.. =/<script type="test/javascript"> $j('body').click(function() { $j('#category_dropdown').slideUp("fast"); return false; });</script>
DerNalia
A: 

Got it

$j(function() {
    $j('body').click(function() {
        $j('#category_dropdown').slideUp("fast");
        return false;
    });

});
DerNalia
Looking at the difference between the code you put in the comment above and this one, I'm assuming that it wasn't working because the body tag was not ready yet when this code was being executed, now you wrapped it up in a document.ready event so it will work :) Remember that jQuery will 'gracefully' fail meaning that if the selector you pass it does not return any elements, it just won't do anything instead of causing errors, this can be misleading as to why something doesn't seem to work when in fact the code was never executed.
SBUJOLD
Also, now that I mentionned about the ready event, the `live()`method to bind handler will often solve that problem and is really useful on very dynamic pages.
SBUJOLD