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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:50:44+00:00 2026-05-28T15:50:44+00:00

Final working code based on accepted answer $(‘#photo-upload, #photo-upload .close’).click(function(){ $(‘#photo-upload’).removeClass(‘show’); }); $(‘#upload-button’).click(function(){ $(‘#photo-upload’).addClass(‘show’);

  • 0

Final working code based on accepted answer

        $('#photo-upload, #photo-upload .close').click(function(){
            $('#photo-upload').removeClass('show');
        });

        $('#upload-button').click(function(){
            $('#photo-upload').addClass('show');
        });

        $('#form-window').click(function(e){
              e.stopPropagation();
        });

original question

I have a modal window with a close button. The window is triggered by a link a#upload-button. #photo-upload is a 100% height + width fixed div. #form-window is a 200px by 200px box that is centered horizontally and vertically inside of #photo-upload

<div id="photo-upload">
    <div id="form-window">
        <!-- Content -->
        <a class="close"></a>
    </div>
</div>

The close button is absolutely positioned a little bit beyond the upper right corner. I want to set a click function so that clicking outside the modal window closes the window, and also so that clicking on the close button closes the window. But so that clicking on the window itself does not close the window.

My current code is:

$('#photo-upload').click(function(e){
    if(!$(e.target).is('#form-window')){
      return false;
    } else {
        $(this).removeClass('show');
    }
});

$('#form-window').live("click",function(e){
    if(!$(e.target).is('a.close')){
      e.stopPropagation();
    }
});


$('#upload-button').live("click", function(){
    $('#photo-upload').addClass('show');
});

$('#photo-upload .close').live("click", function(){
    $('#photo-upload').removeClass('show');
});

With the above code, nothing happens when I click any of those elements, and can’t close the div. What am I doing wrong?

How do I use stopPropagation() on a parent but exclude a child element (in my case, a.close).

  • 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-28T15:50:45+00:00Added an answer on May 28, 2026 at 3:50 pm

    Just add an additional handler to the close button which also closes the window.

    $(".close").live('click',function(){
                $('#form-window').removeClass('show');
            });
    

    Ideally you probably want to actually add the .close selector in addition to document and only have the function once to avoid code duplication.

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

Sidebar

Related Questions

I'm working on a finite deterministic automaton based on this . From this code:
We are working on a Spring 3.0.5 Web MVC-based application. In our code we
FINAL UPDATE: The second code block is working--I just miss-typed the URL. I'm trying
since monday, i'm working as an intern in a company (final year at college)
I have working code but it seems a bit strange that I have to
I am working on an android launcher based on the stock launcher. I am
I am working on the multifile uploader, and want to set the upload directory
Working with the Google Web Toolkit i have written an application which is based
I am trying to show some views based on certain conditions in getView() in
I'm learning Java and working on a guessingGame project based on a grid and

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.