tags:

views:

669

answers:

5

In Jquery how would i make it so that if i had a div, with different elements inside of it, a select, a search input, etc, that when i click outside of the div, on the page, the div fades out, but i can click on the select and type in the search input and not have it fade? any help is appreciated. -nick

A: 

Try this library http://flowplayer.org/tools/expose.html

Dan
+3  A: 

Many modal dialogs use an partially transparent overlay that covers the entire page indicating that the dialog has focus. I would consider this the best way to accomplish what you want. If you really don't want the page darkened or grayed out, you can always just make the overlay completely transparent. For an example check out Facebox.

Zach Bialecki
A: 

You can bind a click handler on $(document) like

$(document).click(function(){
  $(this).unbind('click');
  $('#menu').fadeOut();
  }
Alex JL
I haven't done much with bind. is your code saying that if you click an element with the class of "close_menu" that "#menu" will fade out?I don't want the user to have to click a close button or anything like that, i just want it to fade if the user clicks outside of that div.
ExodusNicholas
The click.close_menu is not about a css class; it's a named bind. I could have just put 'click' there, or used the .click method. I'll edit it to make it more clear. The click is being bound to the whole document.
Alex JL
wouldn't that fadeout the menu, even if i clicked on the menu itself?
ExodusNicholas
Yeah, I wasn't feeling very with it that day. This came from a project where I built a dropdown menu, so it was supposed to close as soon as you clicked the menu, or outside the menu. Worked for us, probably not for what you were asking. I very much recommend Dark Porter's answer. It's what I was getting at, but fixed.
Alex JL
+5  A: 

Code Duck's answer is incomplete, because you'd need to have it not fade out if you click the DIV itself (only outside). So say your DIV that's supposed to fade out has an ID of "menu".

jQuery("#menu").click(function(){ return false; });
jQuery(document).one("click", function() { jQuery("#menu").fadeOut(); });

The use of one is more concise than the bind/unbind. Also namespaced events aren't really needed here if you use one because there's no need to explicitly unbind a particular click handler on document.

The return false is just saying "stop bubbling this event up to the document."

darkporter
Yep, sounds good to me!
Alex JL
nice, works great! but isn't there a better way than to check each time the user clicks the document? so if i randomly click everywhere on the screen, that's firing the function, isn't it?maybe it's early but i don't understand how your code works, but it does.... mmmmmm
ExodusNicholas
No, when you open the menu you call this code. "One" means it fires once then unbinds itself. After the menu closes click all you want all over the place and nothing is bound or gets called.
darkporter
Hey man, i had this working, and then my hard drive crashed and i lost everything. I'm trying to re-build it but whatever i did with this code isn't working now, should this be the final code? <code> $(document).click(function(){ jQuery("#menu").click(function(){ return false; }); jQuery(document).one("click", function() { jQuery("#menu").fadeOut(); }); }) </code>it works the first time i click anything but the DIV itself, but the second time that i do that, it wont fade out
ExodusNicholas
Yeah, that code I posted needs to run every time you open the div. So like `var d = openSomeDiv(); d.click(...); jQuery(document).click(...);`
darkporter
+1 Thanks! :)..
Senthil
A: 

I know this is an older question, but here's an extension I wrote to add a clickOutside function to elements:

$.fn.extend({
// Calls the handler function if the user has clicked outside the object (and not on any of the exceptions)
clickOutside: function(handler, exceptions) {
    var $this = this;

    $("body").bind("click", function(event) {
        if (exceptions && $.inArray(event.target, exceptions) > -1) {
            return;
        } else if ($.contains($this[0], event.target)) {
            return;
        } else {
            handler(event, $this);
        }
    });

    return this;
}

}

With this you could easily do

$("#menu").clickOutside(function(event, obj) { obj.fadeOut() });
jlorich