Close Modal When Clicked Outside of It - CSS & JS

Presently, I am in the process of creating my own modal for the system. When you click on a package name, the modal pops up and is displayed. I have implemented some JQuery code that should remove the modal when clicking on the background. However, it also hides the modal when clicked directly on the modal itself.

I attempted to assign z-index 3 to the parent element and z-index 4 to the modal, but this approach did not solve the issue. How can I configure it so that clicking on the background makes the modal disappear, but clicking on the modal itself does nothing, allowing users to interact normally with the modal?

Here's a small illustration:

https://i.sstatic.net/mFEn1.gif

The popups are dynamically added to a parent container using Ajax (see below)

<div class="container-fluid pt-5 ">
    <div class="row">
        <div class="col-md-6">
            <h3>{{ __('Packages') }}</h3>
        </div>
    </div>
    <div class="row my-5">
        <div class="col-md-4">
            <div id="package-loading" class="text-center">
                <i class="fas fa-spinner fa-pulse fa-2x color-primary"></i>
            </div>
            <p id="package-error" class="text-danger" style="display: none"></p>
            <div id="packages-wrapper"></div>
        </div>
    </div>
</div>
<div class="popup-list">

</div>

<script>
    // AJAX request for packages data and dynamic content generation goes here
</script>

HTML

SASS

.package-popup {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: none;
    overflow: hidden;
    background-color: $primary_background_transparent;
    z-index: 3;
    .package-popup-dialog {
        max-width: 960px;
        position: relative;
        margin: auto;
        top: 100px;
        background-color: $color-white;
        border-radius: 16px;
        padding: 3rem;
        z-index: 4;
        pointer-events: none;
        .package-popup-content {
            position: relative;
            pointer-events: auto;
            display: flex;
            flex-direction: column;
        }
    }
}

Answer №1

Ensure to inspect the click event target:

$(item).on('click', function(event) {
  if (event.target.classList.contains("shadow")) {
    // Execute close dialog functionality
  }
}

It is advisable to eliminate the pointer-events: none from the css for proper functioning

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

FTP upload is failing to transfer the audio file programmatically

I am having an issue with uploading an .mp3 file using FTP code in C#. The file is uploaded successfully to the server, but when I try to bind it to a simple audio control or view it directly in a browser, it does not function as expected. Interestingly, m ...

Error Occurred while Uploading Images using Ajax HTML Editor with JSON Data

I am facing an issue with my AJAX HtmlEditorExtender, specifically when trying to upload an image. The error message I receive is as follows: JavaScript runtime error: Sys.ArgumentException: Cannot de-serialize. The data does not correspond to valid JSON. ...

sluggish jquery treeview loading

I recently implemented a jQuery treeview from a source I found at Initially, the treeview worked well when there were only a small number of nodes and subnodes. However, as the content grew, the loading time increased significantly to the point where usin ...

Error: The variable <something> has not been defined

Within my GitHub project, an error stating Uncaught ReferenceError: breakpoints is not defined is appearing in the Chrome console. This issue should be resolved by including breakpoints.min.js, but it seems that webpack is somehow causing interference. I ...

Once the ajax jQuery is triggered, I hope to see an image loading for a brief moment of just 1

Can someone assist me with the following code snippet? <script type="text/javascript"> $(function () { $(".a").live("click", function () { var $this = $(this), ProductImageId = $this.data('ProductImageId'); ...

How can I place a div using pixel positioning while still allowing it to occupy space as if it were absolutely positioned?

I successfully created an slds path element with multiple steps. I want to display additional subtext information below each current step, but there might not always be subtext for every step. To achieve this, I created an absolute positioned div containi ...

(node:9263) UnhandledPromiseRejectionWarning: ValidationError: ideas validation failed: Missing required field imageUrl

Encountering the UnhandledPromiseRejectionWarning: ValidationError validation failed: imageUrl: PathimageUrlis required error when attempting to upload an image. However, removing or commenting out the required: true keyword in the model file for imageUrl ...

Show information on a pop-up window using Ajax without having to open a new browser tab

I am currently working on a project that uses the Struts framework. In this project, I need to revamp the UI design. The existing project has a menu tab, and when each menu is clicked, a JSP page opens in a new browser window. However, I want these pages t ...

Javascript returning unexpected characters in image URL from ajax response

Within my Codeigniter web application, I am utilizing an ajax function to fetch data from the database in order to display it on the view. The data retrieved includes an image URL along with other fields. The issue arises when I receive the data within th ...

Impose a delay between the execution of two functions in React.js

Looking for a way to introduce a forced delay between two consecutive function calls. Essentially, what I want to achieve is: a // call func a delay(100) // pause for 100 ms b // call func b Is there a method to accomplish this? Update: attempted a() ...

When inserting <img>, unwanted gaps appear between div elements

When I have <img> in the child divs, there is some space between them and a blue stripe appears under the image. How do I make them stack perfectly without any gaps? I am new to HTML and CSS and trying to learn for marketing purposes. Any help or ad ...

jQuery breaks when working with ASP.NET forms

Essentially, it appears that using an ASP.NET page with the <form runat=server> tag can cause some jQuery scripts to break. To illustrate this issue, consider the following scenario: You have a simple webpage with only a checkbox, like so: <inpu ...

What are some strategies for maintaining a responsive layout with or without a sidebar?

I've made the decision to incorporate a sidebar on the left side of all pages across my website. This sidebar must have the ability to be either hidden or shown, without overlapping the existing content on the page. However, I'm encountering an ...

Tips for modifying HTML template code within a Typescript document using Atom

There appears to be a current trend in Angular development where the template code is embedded within the Angular component, usually found in a Typescript or Javascript file. However, when attempting this approach, I noticed that I am missing html syntax ...

Updating a React JS State using a Parameter

Is it feasible to develop a function that accepts a parameter (either a string of the state name or the actual state) and then assigns the state related to the parameter? SetState(x) { // Suppose x can be any state we have already defined (it sh ...

The HTML table seems to be inexplicably replicating defaultValue values

I'm encountering an issue where, when I use JavaScript to add a new table data cell (td), it ends up copying the defaultValue and innerText of the previous td in the new cell. This is confusing to me because I am simply adding a new HTML element that ...

Value binding with conditional rendering in VueJS 2

My goal is to utilize VueJS 2 to render an inline condition while simultaneously adding a value to a DOM element. I am aware that I can use v-if to control the visibility of elements based on conditions, but how can I achieve an inline condition? For exam ...

I'm facing issues with Angular commands not functioning properly even after installing the Angular CLI and configuring the

Every time I attempt to create a new project using Angular CLI by typing: ng n app I encounter the following error message: C:\Users\Venkateshwarn M\AppData\Roaming\npm\node_modules\@angular\cli\bin\ng: ...

The jQuery library is missing from the bundle.js file

After countless days of struggling with an issue, I have attempted numerous solutions from my searches without success. My tech stack includes Hugo, Webpack, Node.js, and Babel. Currently, everything is compiling, but upon checking the localhost console, I ...

The layout of my webpage shifts when viewed in the browser, however it appears fine in the Coda preview

Hi there, I'm a beginner in the world of coding. I recently encountered an issue with my code that seems to be causing some trouble. When I preview it in Coda, everything looks perfect. However, when I try to open it in Chrome, the layout gets all won ...