Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, how: click anywhere outside of the div, the div fades out

Tags:

jquery

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

like image 397
ExodusNicholas Avatar asked Jan 23 '10 20:01

ExodusNicholas


People also ask

How to fade out div in jQuery?

jQuery fadeOut() SyntaxfadeOut( speed, callback_function); selector: It is used to select the html element on which this fade out effect is being applied. speed: The speed of the fadeout effect in milliseconds. It is an optional parameter and can take values such as “slow” or “fast” or in milliseconds.

How do I hide a div when clicking outside?

To hide an element when clicked outside: Add a click event listener to the document object. On each click, check if the clicked element is outside of the specific element using the contains() method. If the clicked element is outside, hide the original element.

How to set fade out time in jQuery?

The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector).fadeOut(speed,callback); The optional speed parameter specifies the duration of the effect.

How to fade image in jQuery?

fadeIn() function fades in our first image in 1500 milliseconds (or, 1.5 seconds). The . delay() function acts as a counter and waits 3500 milliseconds (or, 3.5 seconds), then the . fadeOut() function fades it out in 1500 milliseconds (or, 1.5 seconds).


1 Answers

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."

like image 146
jpsimons Avatar answered Nov 15 '22 22:11

jpsimons