Home › Forums › JavaScript › JQuery: Hide a div if anything except the div and its content is clicked
- This topic is empty.
-
AuthorPosts
-
December 30, 2011 at 2:31 am #35895bennyjienParticipant
Hi,
Can someone please help me with his?
I want to hide a div when anything except the div and its content is clicked.
How do I do this?Thanks a lot.
December 30, 2011 at 3:13 am #93701itsjustjakeMembercan you post some code for an example??
Is this a modal that your trying to close that is being overlayed?
December 30, 2011 at 4:16 am #93704bennyjienParticipantPlease visit miamiurbanist.com.
There is a green menu bar below the main navigation bar.
When you click on the Neighborhood menu for instance, there will be a pop up div showing up. To hide/close this div, currently you must click the Neighborhood menu again.What I want to do is any clicks except on the div and its content, will hide/close the pop up div.
Thanks for your response.
December 31, 2011 at 10:54 am #93786MottieMemberYou could use hover instead; change this function in your scripts.js file:
/* custom scripts */
$(document).ready(function() {
$('#second-nav > li').hover(function(){
$(this).siblings('li.selected').removeClass('selected').children('div').stop(true, true).fadeOut(250);
$(this).addClass('selected').children('div').stop(true, true).fadeToggle(250);
}, function(){
$('#second-nav > li').removeClass('selected').children('div').stop(true, true).fadeOut(250);
});
});But if you must have it as a click, try this code:
/* custom scripts */
$(document).ready(function() {
$('#second-nav > li > span').click(function(){
$(this).parent('li').siblings('li.selected').removeClass('selected').children('div').stop(true, true).fadeOut(250);
$(this).parent('li').toggleClass('selected').children('div').stop(true, true).fadeToggle(250);
});
$(document).click(function(e){
// not an optimal use of selectors, but it works
if ($(e.target).parentsUntil('#second-nav')[0] !== $('#second-nav').find('li.selected')[0]) {
$('#second-nav > li').removeClass('selected').children('div').stop(true, true).fadeOut(250);
}
});
});January 1, 2012 at 4:38 am #93811bennyjienParticipantThank you very much, Mottie.
This really helps!April 28, 2015 at 7:54 pm #201295gozonjoedaimarParticipantI found this useful…
jQuery(document).click(function(event){ // Check if clicked outside .target-div if (!(jQuery(event.target).closest(".target-div").length)) { // Hide .target-div jQuery(".target-div").hide(); } });
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.