Ending the Overlay

I am using an overlay:

<div id="overlayer" class="overlayer">
<div id="board" class="board"></div>
</div>

Here are the CSS properties I have applied to it:

#overlayer
{
position:fixed;
display:none;
top:0;
left:0;
width:100%;
height:100%;
background: -moz-linear-gradient(
        top,
        #807980 0%,
        #3d323d);
background: -webkit-gradient(
        linear, left top, left bottom, 
        from(#807980),
        to(#3d323d));
        opacity:0.6;
        z-index:9998;
}

#board
{
position:relative;
left:33%;
top:20%;
width:600px;
height:450px;
border-radius:15px;
background-color:white;
z-index:9999;
display:none;
}

To trigger the overlay, this script is used:

<script type="text/javascript>
$(document).ready(function(){
function overlayerOn(){
    $('#overlayer').fadeIn(800);
}

function overlayerOff(){
    $('#overlayer').fadeOut(800);
};

$('#fr').click(function(e){
    overlayerOn();
    var $br = $('#board');
    $br.css('display', 'block');
    });

$('#overlayer').click(function(e){
    overlayerOff();});

});
</script>

However, there is a problem with the closing functionality as it closes when clicking on the div inside the overlay. Any suggestions on how to fix this? Thank you...

Answer №2

This phenomenon is referred to as event propagation, and it can be halted by blocking the click event from moving upwards in the DOM hierarchy using e.stopPropagation(); when interacting with the board element like so:

$('#board').click(function(e){
    e.stopPropagation();
});

Answer №3

If you want to prevent the click event from bubbling up, you can consider inserting this code snippet:


$("#board").click(function(e) {
    e.stopPropagation();
    return false;
});

Is this what you were looking for?

Answer №4

Hey there, a great tip is to make use of the e.target property To improve your close function, try replacing it with the following:

$('#overlayer').click(function(e){
    if (e.target === $('#overlayer')[0]) {
     overlayerOff();
    }
});

Understanding event bubbling is key here, check out this informative resource for a quick grasp on the concept: http://www.quirksmode.org/js/events_order.html

Answer №5

Check out this helpful tutorial for preventing event propagation in nested divs.

$("#container").click(function(e){
   e.stopPropagation(); //This method works on most browsers
});

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

What could be causing the array to display incorrect results in React?

Showing below is a snapshot of my VScode. In this code snippet, I am declaring arr1 as an array consisting of numbers and then performing a reverse operation on it. Click here for input. The issue I am encountering is that the first paragraph in the outpu ...

Tips on setting the default sorting order in AngularJS

I have implemented a custom order function in my controller with the following code: $scope.customOrder = function (item) { var empStatus = item.empState; switch (empStatus) { case 'Working': return 1; case & ...

Dealing with undefined properties in Javascript can cause errors

[ { dateTime: '2016-03-30 05:55:53', value: '4.0' }, { dateTime: '2016-03-30 05:55:55', value: '17.0' }, { dateTime: '2016-03-30 05:55:57', value: '17.0' }, { dateTime: '2016-03-30 06:0 ...

Javascript is coming back with a message of "Access-Control-Allow-Origin" not being allowed

I've been encountering some unusual challenges with my React application related to the Access-Control-Allow-Origin issue. Initially, I had similar issues with the login and registration system which I thought were resolved, but now I'm facing di ...

The value retrieved from redux appears to be varying within the component body compared to its representation in the return

Trying to fetch the most recent history value from the redux store to pass as a payload is presenting a challenge. When submitting a query, the history updates and displays the latest value within the map() function in return(), but when checking at // CON ...

The Uib-Dropdown functionality is not functioning properly when placed within the body of an HTML view

After correctly installing the following dependencies: "ui-bootstrap": "0.12.2", "ngAnimate": "1.5.5", "AngularJs": "1.5.5 I encountered an issue with creating a dropdown menu in my HTML view. Despite no visible errors and successful implementati ...

Troubleshooting: jQuery ajax form is not functioning as expected

After attempting various methods to create a basic jQuery Ajax form, I am puzzled as to why it is not submitting or displaying any notifications. Below is the code I have been working with: Javascript ... <script type="text/javascript" src="assets/js ...

What is the best method to redirect users who are not logged in from every URL when using PassportJs?

I am currently developing a web application using NodeJS, and I have integrated PassportJS with passport-local-mongoose for authentication. Below is the code snippet I've created for the root route to verify if the user is logged in: app.get('/ ...

After ReactDOM is used, the JavaScript code is no longer functioning properly

While working on my latest project, I utilized react.js and encountered an issue where javascript seemed to stop working when using ReactDOM to render a class extended from React.Component. I noticed that the alert() method would not work if it was placed ...

What steps can I take to resolve a Bootstrap form validation issue where the email field is causing the label to shift down?

I am currently troubleshooting an issue in a website using Visual Studio, which is built with VB.NET, ASP.NET, HTML, CSS, and Bootstrap. The problem lies in the input validation under the email addresses. While one validation is working fine, the validatio ...

What is the best way to access and display the innerText of elements that have been removed using console

When I call the updateCartTotal() function, my goal is to display in the console the items that have been removed from the shopping cart. Every time I click on the remove button, I want it to show the item and the price. However, instead of displaying the ...

The issue of AJAX .done() method returning undefined when using $.each() on JSON response

I am having trouble retrieving the JSON response from an $.ajax() request. After selecting a car model, I receive data from the API, but I am unable to access the JSON results to populate a new drop-down list with the required information. HTML <div cl ...

What is AngularJs's preference: URL or ID for templateUrl?

When looking at the AngularJS documentation, you will come across an example using the templateUrl property: In the HTML: //ngApp... <div ng-controller="Ctrl"> <div my-customer></div> </div> And in the controller: . ...

I am having trouble styling a pre-existing class in bootstrap using my custom CSS file

html <section id="title"> <div class="container-fluid"> <!-- Nav Bar --> <nav class="navbar navbar-expand-lg navbar-dark"> <a class=" navbar-brand">tindog</a> ...

An issue with the animation script in Ionic

I'm having trouble converting this to an Ionic application. Here's my code sample. Can anyone help me with putting this code correctly in Ionic? I'm trying to achieve something like this example <script src="https://cdnjs.cloudflare.com ...

Moving from the landing page to a specific tab in Ionic 2+ using dynamic navigation

Upon launching the application, users are greeted with a landing page called IntroPage, featuring two prominent buttons labeled BUY and SELL. Clicking on either of these buttons will navigate users to the corresponding buy or sell Tab. In my quest to achi ...

Instructions for saving a binary file on the client using jQuery's .post function

I am working with a handler that has the following code: HttpRequest request = context.Request; HttpResponse response = context.Response; if (request["Type"] != null) { try { string resultFile = null; ...

Extracting JSON Data into Text Boxes within jQuery UI Dialogs

Let me provide some context to my current issue. Due to the nature of the problem being work-related, I am unable to share much of the code. The task at hand involves working with a jQuery dialog. Essentially, I have a list of names displayed with a boots ...

Change a TypeScript alias within the @types namespace

While using Typescript 3, I encountered a situation where I needed to modify a type alias from the @types/json-schema definition provided by DefinitelyTyped. The issue arose when I wanted to incorporate a custom schema type into my code. Since this custom ...

Executing JavaScript function on AJAX update in Yii CGridView/CListView

Currently, I am integrating isotope with Yii for my CListView and CGridView pages to enhance their display. While everything functions smoothly, an issue arises when pagination is utilized, and the content on the page is updated via ajax, causing isotope ...