Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:42:19+00:00 2026-06-15T21:42:19+00:00

So i am working on this simple script which should hide any object when

  • 0

So i am working on this simple script which should hide any object when you have clicked outside of it.

Here is jsFiddle and my simple code:

HTML

<div class="container">
    Click anywhere in this div

    <div style="border:1px solid blue; float:right;">div should still open/close</div>
</div>

<div class="close_me">Close me!</div>​

JS

$(".container").click(function(){
    $(".close_me").toggle();
});


helper_close(".close_me", ".container");

//this is simplified example of my function
function helper_close(target, avoid){
    //note that this function will be called once per object
    //so it should be fine to bind new event on document
    $(document).on("click", function(event){

        if( $(event.target).is(avoid) )
            return true;

        $(target).hide();
    });
}

​Now as you can see it works, (kind of) the problem is that close_me should also appear/disappear when you have clicked anywhere inside of container, which as you can see doesn’t happen right now.

I know that i could just specify every object i want to “avoid” but what if i have 20 divs inside that container? Well the code would look very ugly… So i need some better way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:42:20+00:00Added an answer on June 15, 2026 at 9:42 pm

    Just check to see if the clicked element has a parent somewhere up the chain matching the “avoid” selector, if not, hide :

    $(document).on("click", function(event){
        if(! $(event.target).closest(avoid).length ) $(target).hide();
    });
    

    FIDDLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple script that I'm working on. I must admit, I'm totally
Why is this simple HTML document not working right? What have I missed? I
I have this simple example I can't seems to get working : MERGE INTO
Simple question, how make this code working ? public class T { public static
I have written this short script (which I've stripped away some minor detail for
Okay, so I have this partially working game, I know the solutions should be
For some reason the script below is not working. This is the code I
This should be fairly simple, but I seem to be missing something. I'm working
why this simple query not working. I know it was OK before I upgraded
I am tired to find out why this simple query not working. SELECT image_url

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.