jQuery hiding a div on click outside

Posted by Lee on Stack Overflow See other posts from Stack Overflow or by Lee
Published on 2010-05-02T15:23:33Z Indexed on 2010/05/02 15:27 UTC
Read the original article Hit count: 894

Filed under:
|
|

Hi All

I would like to build a simple menu for each list element clicked on but hide this div once you click outside it. Here is some simple code which hopefully will make sense.

$('.drillFolder').click(function(){
    var id = $(this).attr('data-folder');
    $(".drillDownFolder ul li > a").attr('data-id', id);
    $(".drillDownFolder").show();
});

$("body").click(function(e){
    if(e.target.className !== "drillDownFolder")
    {
      $(".drillDownFolder").hide();
    }       
});

//The hidden div
<div class="drillDownFolder" style="display:none">
    <ul>
        <li><a href="#" data-id="">Show Image</a></li>
        <li><a href="#" data-id="">Edit Image</a></li>
    </ul>
</div>

I know whats wrong as the menu is show via the .drillFolder links the body click is then hiding it straight away. How can I avoid this.

Thank you if you can advise

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about show-hide