-2

I write this code to hide div when user click anywhere in body outside this div.. but is wrong what is the problem ?

$('body').click(function() {
   $('.mydiv').hide();
});
Abudayah
  • 3,816
  • 7
  • 40
  • 60
  • http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it –  Mar 16 '13 at 09:42

3 Answers3

5

If u give like this

$('body').click(function() {    
    $('.mydiv').hide(); 
});

This DIV will get Hidden even if you click inside the DIV.

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Manimaran
  • 76
  • 1
  • 5
0

Have you tried replacing

hideMenu()

with

$('#cMenu').hide();
JesseBuesking
  • 6,496
  • 4
  • 44
  • 89
0

Instead of giving that function to the body onclick event, you can make two container divs: One for everything in the body above your menu, and one for everything below your menu within the body tag, and give those two divs the onclick function you're currently giving to the body.

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119