tags:

views:

4947

answers:

3

I am using this code:

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

 $('.form_wrapper').click(function(event){
 event.stopPropagation();
 });


<div class="form_wrapper">
<a class="agree" href="javascript:;">I Agree</a>
<a class="disagree" href="javascript:;">Disagree</a>
</div>

The problem is that I have links inside the DIV and when they no longer work when clicked.

+4  A: 

You might want to check the target of the click event that fires for the body instead of relying on stopPropagation.

Something like:

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

Also, the body element may not include the entire visual space shown in the browser. If you notice that your clicks are not registering, you may need to add the click handler for the HTML element instead.

David Andres
Yep, now the links work! But for some reason, when I click the link, it fires it twice.
Scott
Ah.. something in my code. Hey great solution!
Scott
+11  A: 

You'd better go with something like this:

var mouse_is_inside = false;

$(document).ready(function()
{
    $('.form_content').hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });

    $(body).mouseup(function(){ 
        if(! mouse_is_inside) $('.form_wrapper').hide();
    });
});
Makram Saleh
Yes... I will try this. Thanks!
Scott
Thank you for your answer man! Helped a lot!
Ricardo
+3  A: 
$(document).click(function(event) {
    if ( !$(event.target).hasClass('form_wrapper')) {
         $(".form_wrapper").hide();
    }
});
meder
Hmmm... If I click on something INSIDE the div, the entire div disappears for some reason.
Scott
Instead of checking if the target has the class, try: if ( $(event.target).closest('.form_wrapper).get(0) == null ) { $(".form_wrapper").hide(); }This will insure that clicking things inside of the div won't hide the div.
John Haager