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 6929641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:20:38+00:00 2026-05-27T11:20:38+00:00

The question of how to detect a click on anywhere except a specified element

  • 0

The question of how to detect a click on anywhere except a specified element has been answered a couple of items like here:

Event on a click everywhere on the page outside of the specific div

The problem I have is trying to figure out how to detect a click anywhere except a given element including one of it’s children.

For example in this code:

http://jsfiddle.net/K5cEm/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(function() {

$(document).click(function(e) {
   $('#somediv').hide();
});
$('#somediv').click(function(e) {
   e.stopPropagation();
});

});
</script>

<div style="border: 1px solid red; width:100px; height: 100px" id="somediv">

    <span style="display: block; border: 1px solid green; width:50px; height: 50px; margin: 0 auto" id="someSpan"></span>

</div>

Clicking anywhere outside the red div should cause it to hide. Not only that but also clicking on it’s child element (the green span) should cause it to hide. The only time it shouldn’t hide is if you click on it but not on the span. As it stands now, the click on the span is also considered a click on the parent div hence it doesn’t hide the div if the span is clicked.

How to achieve 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-05-27T11:20:39+00:00Added an answer on May 27, 2026 at 11:20 am

    You can compare the click’s target to the element in question:

    $(document).click(function(e) {
        if (e.target != $('#somediv')[0]) {
            $('#somediv').hide();
        }
    });
    

    Demo: http://jsfiddle.net/K5cEm/7/

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

Sidebar

Related Questions

My question is not like this one: Browser-independent way to detect when image has
Update I summarized the question and its answers here My objective is to detect
Quick question - is it possible to detect whether a focus has come from
How can i detect external link click? I have a simple question, which may
My question is the folowing Is there a solid javascript code to detect if
I think the question title seems to explain eveything. I want to detect whether
I am using jQuery to detect drop on target, the target in question is
I cannot find a simple example about my question above: how can i detect
Beginner here, I have a simple question. In Android what would be the best
Related question: How to detect integer overflow? In C code, should integer overflow be

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.