[jQuery] Re: Hide/Close div when clicked outside of it.
Hello, for those wondering, if you want to know whenever you click outside a div and byt outside i mean everywhere but the div like you want to display an error message : html head titletest error click outside/title style type=text/css p.error { position:absolute; top:40%; left:30%; width:40%; /style /head body blaeblalblpdsqdsq/pspanfsqf/span div id=onefsqfqsf/div p class=errorspanERROR :/span emClick outside this paragraph to close it, you can even click on me !/em/p div clas=fslfqs/div !-- THIS IS THE PART THAT YOU WANT TO LOOK AT ;) -- script type=text/javascript $(document).bind('click', function(e) { var $clicked=$(e.target); // get the element clicked if($clicked.is('.error') || $clicked.parents().is('.error')) { // if the element clicked is the one we're looking for or if the element has a parent that we're looking for then we clicked the right place ! console.log('BULLSEYE !'); // here you should do anything } else { // we're outside the error paragraph, no matter where but we're outside ! console.log('you clicked outside the error paragraph, congrats !'); //here you should close the message :) } }); /script /body /html This works in FF, IE7 (expect for the console.log thing ...), didnt tested it in ie6 etc... But think it works. If you just want to close it then : script type=text/javascript $(document).bind('click', function(e) { var $clicked=$(e.target); // get the element clicked if( ! ( $clicked.is('.error') || $clicked.parents().is('.error') ) ) { $('.error').hide(); } }); /script Let me know if that work for you On 2 mai, 20:35, Aleksandr [EMAIL PROTECTED] wrote: Everything is working now. Thanks to all. Last version of code is: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }); $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 7:29 pm, Josh Nathanson [EMAIL PROTECTED] wrote: Looks like you are missing a semicolon which will choke IE: $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) -- ADD SEMICOLON HERE -- Josh - Original Message - From: Aleksandr [EMAIL PROTECTED] To: jQuery (English) jquery-en@googlegroups.com Sent: Friday, May 02, 2008 11:00 AM Subject: [jQuery] Re: Hide/Close div when clicked outside of it. I am now have this jQuery code and it is working in FireFox, Safari and Opera but not in IE 6, 7: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 6:00 pm, sawmac [EMAIL PROTECTED] wrote: try this $('#divLoginBox1').click(function(e) { e.stopPropagation();}); $(document).click(function() { $('#divLoginBox1').hide(); }); --dave
[jQuery] Re: Hide/Close div when clicked outside of it.
I was try to do in this way. But it also close div clicking inside of it. So, if I click in wrapper area it close divLoginBox1. But if I clicked divLoginBox1 area it close it also. Should be the way how exclude divLoginBox1 area from wrapper div. This line not help to do it: $(#wrapper).not('#divLoginBox1') On May 1, 10:14 pm, Jimslam [EMAIL PROTECTED] wrote: The Easiest method would be to create a wrapper div that takes up the entire screen and assign an onClick event to that, which would then contain the other div. For example: div id=wrapper div id=divLoginBox1 Login box content /div /div The CSS for the DIV id=wrapper would be something like: #wrapper { position:absolute; height:100%; width: 100%; } Then the JS would be: $(#wrapper').click(function() { $('#divLoginBox1').hide(); }); That should do it. You could leave the #wrapper background transparent or add in some opacity to a background color to emulate a faded effect to the page content. I believe this is similar to how well established libraries like thickbox handle the same functionality. On May 1, 11:14 am, Aleksandr [EMAIL PROTECTED] wrote: Yes you right. Also I have close link inside of the div. Everything is working, only outside click left. On May 1, 3:08 pm, Wes Duff [EMAIL PROTECTED] wrote: Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
So the other option is to have the wrapper not contain the divLoginBox1 so that clicking inside of divLoginBox1 would not be clicking inside of wrapper. Just be sure to set the z-index on divLoginBox1 higher. On May 2, 4:21 am, Aleksandr [EMAIL PROTECTED] wrote: I was try to do in this way. But it also close div clicking inside of it. So, if I click in wrapper area it close divLoginBox1. But if I clicked divLoginBox1 area it close it also. Should be the way how exclude divLoginBox1 area from wrapper div. This line not help to do it: $(#wrapper).not('#divLoginBox1') On May 1, 10:14 pm, Jimslam [EMAIL PROTECTED] wrote: The Easiest method would be to create a wrapper div that takes up the entire screen and assign an onClick event to that, which would then contain the other div. For example: div id=wrapper div id=divLoginBox1 Login box content /div /div The CSS for the DIV id=wrapper would be something like: #wrapper { position:absolute; height:100%; width: 100%; } Then the JS would be: $(#wrapper').click(function() { $('#divLoginBox1').hide(); }); That should do it. You could leave the #wrapper background transparent or add in some opacity to a background color to emulate a faded effect to the page content. I believe this is similar to how well established libraries like thickbox handle the same functionality. On May 1, 11:14 am, Aleksandr [EMAIL PROTECTED] wrote: Yes you right. Also I have close link inside of the div. Everything is working, only outside click left. On May 1, 3:08 pm, Wes Duff [EMAIL PROTECTED] wrote: Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
z-index of divLoginBox1 is higher than wrapper div. On May 2, 3:52 pm, Jimslam [EMAIL PROTECTED] wrote: So the other option is to have the wrapper not contain the divLoginBox1 so that clicking inside of divLoginBox1 would not be clicking inside of wrapper. Just be sure to set the z-index on divLoginBox1 higher. On May 2, 4:21 am, Aleksandr [EMAIL PROTECTED] wrote: I was try to do in this way. But it also close div clicking inside of it. So, if I click in wrapper area it close divLoginBox1. But if I clicked divLoginBox1 area it close it also. Should be the way how exclude divLoginBox1 area from wrapper div. This line not help to do it: $(#wrapper).not('#divLoginBox1') On May 1, 10:14 pm, Jimslam [EMAIL PROTECTED] wrote: The Easiest method would be to create a wrapper div that takes up the entire screen and assign an onClick event to that, which would then contain the other div. For example: div id=wrapper div id=divLoginBox1 Login box content /div /div The CSS for the DIV id=wrapper would be something like: #wrapper { position:absolute; height:100%; width: 100%; } Then the JS would be: $(#wrapper').click(function() { $('#divLoginBox1').hide(); }); That should do it. You could leave the #wrapper background transparent or add in some opacity to a background color to emulate a faded effect to the page content. I believe this is similar to how well established libraries like thickbox handle the same functionality. On May 1, 11:14 am, Aleksandr [EMAIL PROTECTED] wrote: Yes you right. Also I have close link inside of the div. Everything is working, only outside click left. On May 1, 3:08 pm, Wes Duff [EMAIL PROTECTED] wrote: Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
try this $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); --dave
[jQuery] Re: Hide/Close div when clicked outside of it.
I am now have this jQuery code and it is working in FireFox, Safari and Opera but not in IE 6, 7: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 6:00 pm, sawmac [EMAIL PROTECTED] wrote: try this $('#divLoginBox1').click(function(e) { e.stopPropagation();}); $(document).click(function() { $('#divLoginBox1').hide(); }); --dave
[jQuery] Re: Hide/Close div when clicked outside of it.
Looks like you are missing a semicolon which will choke IE: $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) -- ADD SEMICOLON HERE -- Josh - Original Message - From: Aleksandr [EMAIL PROTECTED] To: jQuery (English) jquery-en@googlegroups.com Sent: Friday, May 02, 2008 11:00 AM Subject: [jQuery] Re: Hide/Close div when clicked outside of it. I am now have this jQuery code and it is working in FireFox, Safari and Opera but not in IE 6, 7: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 6:00 pm, sawmac [EMAIL PROTECTED] wrote: try this $('#divLoginBox1').click(function(e) { e.stopPropagation();}); $(document).click(function() { $('#divLoginBox1').hide(); }); --dave
[jQuery] Re: Hide/Close div when clicked outside of it.
Everything is working now. Thanks to all. Last version of code is: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }); $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 7:29 pm, Josh Nathanson [EMAIL PROTECTED] wrote: Looks like you are missing a semicolon which will choke IE: $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) -- ADD SEMICOLON HERE -- Josh - Original Message - From: Aleksandr [EMAIL PROTECTED] To: jQuery (English) jquery-en@googlegroups.com Sent: Friday, May 02, 2008 11:00 AM Subject: [jQuery] Re: Hide/Close div when clicked outside of it. I am now have this jQuery code and it is working in FireFox, Safari and Opera but not in IE 6, 7: $(document).ready(function(){ $(#link).click(function(){ if ($(#divLoginBox1).is(':hidden')) $(#divLoginBox1).show(); else{ $(#divLoginBox1).hide(); } return false; }) $('#divLoginBox1').click(function(e) { e.stopPropagation(); }); $(document).click(function() { $('#divLoginBox1').hide(); }); }); On May 2, 6:00 pm, sawmac [EMAIL PROTECTED] wrote: try this $('#divLoginBox1').click(function(e) { e.stopPropagation();}); $(document).click(function() { $('#divLoginBox1').hide(); }); --dave
[jQuery] Re: Hide/Close div when clicked outside of it.
I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
Yes you right. Also I have close link inside of the div. Everything is working, only outside click left. On May 1, 3:08 pm, Wes Duff [EMAIL PROTECTED] wrote: Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
The Easiest method would be to create a wrapper div that takes up the entire screen and assign an onClick event to that, which would then contain the other div. For example: div id=wrapper div id=divLoginBox1 Login box content /div /div The CSS for the DIV id=wrapper would be something like: #wrapper { position:absolute; height:100%; width: 100%; } Then the JS would be: $(#wrapper').click(function() { $('#divLoginBox1').hide(); }); That should do it. You could leave the #wrapper background transparent or add in some opacity to a background color to emulate a faded effect to the page content. I believe this is similar to how well established libraries like thickbox handle the same functionality. On May 1, 11:14 am, Aleksandr [EMAIL PROTECTED] wrote: Yes you right. Also I have close link inside of the div. Everything is working, only outside click left. On May 1, 3:08 pm, Wes Duff [EMAIL PROTECTED] wrote: Let me see if I have this clear. Once I do I will write you up a script. When someone clicks a link a href=#Show Login Box/a you want to display a div that shows the login box. When someone clicks somewhere else on the screen and not on the link you want to hide the login box? On May 1, 5:08 am, Aleksandr [EMAIL PROTECTED] wrote: I still have the same issue. Yes, I have wrapper div how it can be solved in this case? Thanks On Apr 30, 7:05 pm, Wes Duff [EMAIL PROTECTED] wrote: Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex
[jQuery] Re: Hide/Close div when clicked outside of it.
Off the top of my head --- Try something like this $('div#mydiv').clcik(function(){$('div#mydiv').show();}); //click to show div $('div:not(#mydiv)').click(function(){$('div#mydiv').hide()}); click anywhere else to hide div ## Problem just thought of If you are using a wrapper div then you will have the same problem as before. Well you get the idea. This is just off the top of my head but if it donst work you get the idea. On Apr 30, 11:17 am, Aleksandr [EMAIL PROTECTED] wrote: Hi All, I am showing a div on click of a hyperlink. Now, when i click elsewhere in the document other than the div itself, then i want to hide the showing div... Is there any easy way to do this? I've already try: $('html').click(function() { $('#divLoginBox1').hide(); }); and $('body').click(function() { $('#divLoginBox1').hide(); }); but this close div when clicked inside of it. Thanks, Alex