Issues with jQuery .on('click') arise when employing absolute positioning

I am currently working on a website project where I have implemented a popup element with a dark backdrop. However, the issue I am facing is that when clicking within the popup itself, it also triggers the fade-out effect instead of just when clicking on the backdrop.

Any suggestions on how to resolve this? Check out the JsFiddle for reference.

This is the problematic HTML section:

<div class="popup-container" data-reference="sign-in">
    <div class="sign-in">
        <h1>Sign In</h1>
        <div class="popup-content-container">
        <form class="sign-in" method="post" action="./action.php">
            <input type="text" name="username" placeholder="Username" /><br />
            <input type="password" name="password" placeholder="Password" /><br />
            <input type="submit" value="Sign In" /><br />
        </form>
        <div class="footer">
            <a href="#">Forgotten Username?</a> <a href="#">Forgotten Username?</a><br />
            <a href="#">Don't Have an Account?</a>
        </div>
    </div>
    </div>
</div>

Current jQuery code snippet:

$('div.popup-container').on('click', function() {
    $(this).fadeOut();
});

Current CSS related to the HTML section:

div.popup-container {
    position:absolute;
    margin:auto auto;
    top:0;bottom:0;
    left:0;right:0;
    background:rgba(0,0,0,0.5);

    z-index:101;
}
    div.popup-container:hover {
        cursor:pointer;
    }
/*----  Sign In ----*/
div.sign-in {
    width:400px;
    height:300px;
    position:absolute;
    margin:auto auto;
    top:0;bottom:0;
    left:0;right:0;
    background:#FFF;
    border:solid 1px #CCC;
    border-radius:5px;

    z-index:111;
}
    div.sign-in:hover {
        cursor:default;
    }
    div.sign-in > h1 {
        display:block;
        padding:15px 0;
        border-bottom:solid 1px #CCC;
        text-align:center;
    }
    and so forth...

In summary: Why does my use of .on('click') trigger both inside and outside the popup?

Answer №1

One possible resolution is as follows:

$('div.popup-container').on('click', function() {
    $(this).fadeOut();
});

$('div.sign-in').on('click', function(e) {
    e.stopPropagation();
});

By implementing this code snippet, it prevents child elements from triggering any click events that are attached to the parent element (popup-container) automatically.

To learn more about event.stopPropagation(), visit this link.

Access the demo on jsFiddle here.

Answer №2

Feel free to give this code a try:

$(document).on('click','div.popup-container' ,function() {
   $(this).fadeOut();
});

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

Utilizing ASP.NET MVC3 to efficiently return multiple JSON lists

My ASP.NET MVC3 C# code is returning a JSON list like this: return Json(new { name = UserNames, imageUrl = ImageUrls }); The variables UserNames and ImageUrls are both of type List<string>. Here is my JavaScript function: function StartSearch(tex ...

HTML - Using Tables to Add and Edit Rows

Take a look at this demo on JSFiddle for my project: Demo jsfiddle I'm currently in the process of creating a table with various functionalities. The function "sortTable" is responsible for sorting the table and it's functioning as expected. T ...

Display the current index carousel caption from Bootstrap 4 in a separate div

I'm attempting to showcase the text from the carousel in Bootstrap 4 within another container. Currently, I can retrieve the current index and the text individually, but I am struggling to combine them together. Below is my code, would appreciate an ...

When using the "top" property in position-absolute, the child element may exceed the boundaries of the parent element

My child element is overflowing its parent when I apply "top: random pixel" to it. It seems to be using the top position relative to the body of the page. // HTML <div class="movie-list"> <div class = "inn ...

Altering the style of the underline for a hyperlink

I'm looking to customize the underline style when hovering over a link. Specifically, I want to change the color and size of the underlined link. const useStyles = makeStyles(theme => ({ button: { marginLeft: theme.spacing(2), }, }) ...

Display data from additional tables within a loop

Within my database, I have organized information into 3 tables: train_information: +----------+-----------------+------------------+ | train_id | number_of_axles | number_of_bogies | +----------+-----------------+------------------+ | 1 | ...

What technique does Wordpress use to achieve this understated 3D appearance?

This unique effect that I've come across on various websites is quite subtle. I'm curious about how it achieves this effect. It seems like more than just a border, but also involves the empty space. ...

I want the hover effect to trigger specifically when I hover over the image within a column of a row using CSS

My issue lies with the hover effect on my webpage. Currently, the problem I am facing is that the hover effect activates whenever my mouse hovers over any part of the column in a row. How can I make it so that the effect only triggers when hovering over th ...

Tips for incorporating animation while altering element styles within JavaScript

I am looking to incorporate a sliding animation that moves the element downward when the display property is set to block, and upward when it's set to none or an empty string, here is the current JavaScript code I have: <script> function showQ ...

Creating a Vue application without integrating Bootstrap into the build process

Currently, I am in the process of learning how to develop a Vue app, but I am encountering some difficulties regarding Bootstrap and JQuery integration. The platform where the app is meant to be deployed already has Bootstrap and JQuery integrated into it ...

Issue with Angular: boolean value remains unchanged

Currently, I'm encountering an issue with my application. My objective is to establish a list containing checkboxes that toggle their values between true and false when clicked. Sounds simple enough, right? Below is the HTML code snippet: <l ...

What is preventing the ":last-child" selector from working when the parent element is <body>?

Here is a basic HTML structure: <body> <a> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </a> </body> The divs are nested within ...

What is the best way to execute a function before showing an alert in a $.post() callback?

Just to clarify, the code I'm working with is not original to me; I am simply making some interface adjustments using a plugin for an existing system. The task at hand involves creating a plugin that utilizes blockUI (yes, a plugin within a plugin) t ...

What is the specific use of the second app.css file in Laravel?

I'm currently delving into Laravel 8, and I'm perplexed by the role of Resources\css\app.css. While every other component in the Resources directory serves a clear function, the app.css file appears to be empty. Even when I attempt to a ...

Strange Behavior of Anchor Tags

I've been struggling with my anchor tag not working properly. The desired URL is localhost/VShroff/home/about.php, but it keeps changing to localhost/about.php without redirecting. I've spent a long time trying to figure this out. Here is the HT ...

What is the best way to replicate touch functionality on mobile browsers for both Android and iPhone devices?

Recently, while developing a web application, I ran into an issue on mobile browsers where the :active pseudo class wasn't functioning properly. I am currently utilizing CSS sprites and looking for guidance on how to simulate clicks for mobile browser ...

Modifying background with JavaScript and AJAX技术

I currently have a table structured like the following; <table style="width: 100%; border: solid 1px #666600; min-width: 800px" cellpadding="0" cellspacing="0"> <tr> <td id="aaa">&nbsp;</td> ... Using jQuery's ajax functi ...

What is the process for transmitting data using an Ajax Query to a Controller and subsequently displaying a new JSP page returned by the Controller?

One issue I am facing involves sending data to a Spring Boot Controller through an AJAX Query and then loading a new JSP page. When I send data to the url in the AJAX Query, which matches the URL in my controller class, it executes the code within that met ...

Using a class variable to access canvas methods in Javascript

As someone new to Javascript, I am facing a bit of confusion when it comes to classes and objects. Recently, I refactored some code into a working class but the process did not go as smoothly as I had hoped. Despite searching through Stackoverflow, Google ...

What is the best way to incorporate a YouTube channel into a webpage once the document is fully loaded?

I have a YouTube embedded channel from a particular company, and below I have links to other related companies. However, this is just a widget on my page. When I click a link in the list, I want to load the new embedded YouTube channel from the other compa ...