Hide div when clicking outside

Posted by scferg on Stack Overflow See other posts from Stack Overflow or by scferg
Published on 2011-06-22T23:41:44Z Indexed on 2011/06/23 0:22 UTC
Read the original article Hit count: 79

Filed under:
|

I've searched and search not only google but also here and still have to find a solution that will work.

I have a div that is hidden by default, then toggled by clicking on a link. I also want it to hide when you click outside of the div. Simple, I thought, but nothing I've tried had worked yet.

This is the jquery:

<script type="text/javascript">
    function toggleDiv(divID) {
    $("#"+divID).fadeToggle(200);
    }
</script>

And the link that toggles it:

<a onclick="toggleDiv('myDiv');">Link</a>

And then the div:

<div id="myDiv">
stuff
</div>

If anyone has any advice, I would really appreciate it.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about hide