views:

39

answers:

4

Hello,

I have a div that opens when user clicks link. And the div sort hovers over the link (its a more info box/div) and the div has a links and text. Now what I want is that when user cliks outside of the div it closes/disappears. But I want the links inside of div to work. Atm the javascript for that closin is like this:

$('html').click( function() {
   $('#moreInfo').hide();                                           
});

But the problem is that when user clicks the link inside of that #moreInfo the link doesn't work and the div just closes (it should go to different page from that link, not close the div).

+2  A: 

You can do this:

$(document).click(function() {
  $('#moreInfo').hide();                                           
});
$('#moreInfo').click(function(e) {
  e.stopPropagation();
});

By using event.stopPropagation() on the click handler for the <div>, clicks coming from inside won't bubble up to where you have a handler to close it...which is what's currently happening.

Nick Craver
A: 

If I understood correctly, you don't want to hide the DIV, you want to remove it from the DOM tree.

If this is the case, try this:

$('#moreInfo').remove();

Just remember to keep the reference to the item, so that you can re-add it when you need to.

Rodrigo Gama
A: 

Maybe try something like:

$('not:(#moreInfo)').click( function() {
   $('#moreInfo').hide();                                           
});
jtabak
The syntax is a bit off (should be `:not()`) but I highly recommend *not* doing this, it won't solve the issue and it'll bind a click handler to every element in the DOM :)
Nick Craver
I don't know how JQuery implemented the `not:`, but being naive doesn't this just adds the click handler to *every single* object? After all $( '..' ) is a selector, so it should return all other elements on which then the click handler is added... If that's the case, then this is actually a very bad solution. /edit: Okay, thanks for the confirmation Nick ;)
poke
A: 

Hi,

I recommend you to put a closing X on the up right corner of your DIV (like a window)... In other case, you can handle blur event of a "special" element inside your div.

Hope that helps,

Ramon Araujo