code_junkie

Event on a click everywhere on the page outside of the specific div

Nov 12th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. $(document).click(function(e) {
  2. $('#somediv').hide();
  3. });
  4.  
  5. $('#somediv').click(function(e) {
  6. e.stopPropagation();
  7. });
  8.  
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  10. <html>
  11. <head>
  12. <title>Untitled Document</title>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  14. <meta http-equiv="Content-Style-Type" content="text/css">
  15. <meta http-equiv="Content-Script-Type" content="text/javascript">
  16. <style type="text/css">
  17. <!--
  18. #mydiv{
  19. background-color: #999999;
  20. height: 100px;
  21. width: 100px;
  22. }
  23. -->
  24. </style>
  25. <script type="text/javascript">
  26. document.onclick=check;
  27. function check(e)
  28. {
  29. var target = (e && e.target) || (event && event.srcElement);
  30. var obj = document.getElementById('mydiv');
  31. if(target!=obj){obj.style.display='none'}
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <div id="mydiv">my div</div>
  37. </body>
  38. </html>
Add Comment
Please, Sign In to add comment