Click on the div and any elements it contains in the CSS and JavaScript

I want to create a partially transparent overlay that covers an entire web page, similar to the example here:

<div id="overlaySplash" onclick="clickHandler(this)">
    <div id="insideBox">
   [ insert clickable elements here with onclick="dosomething()" ]
    </div>
</div>

css

#overlaySplash {
    position: absolute;
    top: 0;
    left: 0;
    bottom:0;
    right:0;
    background: rgb(50, 50, 50);
    background: rgba(0, 0, 0, .50);
    z-index: 50;
}
#insidebox {
    position: absolute;
    z-index: 100;
    left: 15%;
    right: 15%;
    bottom: 5%;
    line-height: 50px;
    text-align: left;
}

The issue I'm facing is that I am not using jQuery and the overlay div contains clickable elements. When I click on an element inside the main overlay div, JavaScript returns the ID of the overlay div itself, not of the clicked element. This makes it difficult to determine where exactly the user clicked.

function clickHandler(e){ //hide the div overlaySplash

  e = e || event; 

  alert(e.id);
}

THE BOTTOM LINE:
If the user clicks on a label inside the div: dosomething();
If the user clicks on the background (the DIV itself): closeOverlaySplash();

Answer №1

Instead of completely halting the propagation, consider leaving room for potential future use. It might be more convenient to organize this functionality in separate JavaScript and CSS files for better manageability.

The problem at hand is essentially the event bubbling up to the parent unnecessarily. By checking the event.target.id, you can determine whether the click occurred on the parent or the overlay itself.

For example:

if (event.target.id == "overlay") { 
    //hide the overlay
}

JS Playground

Answer №2

Here is an example:

Sample Code

<div id="overlaySplash" onclick="clickHandler(event);">
    <div id="insideBox" onclick="clickHandlerInner(event);">Inner</div>
</div>

JavaScript Function

function clickHandler(event) {
    alert("no");
}
function clickHandlerInner(event) {
    if (!event) var event = window.event;
    event.cancelBubble = true; //For IE
    if (event.stopPropagation) event.stopPropagation()
    alert("yes")
}

Link to jsFiddle

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

Using flexbox css to arrange content on the top, bottom, and right side

Just getting started with flexbox and I'm trying to position content on the top, bottom, and right side. Check out the demo However, I also need to adjust it for mobile view like the image shown below: https://i.sstatic.net/1YxtZ.png The left and r ...

dividing JavaScript files in the three.js game development platform

After spending two years teaching myself unity/c#, I believe I have the coding skills to transition from an engine to a framework for better control over the environment. As I started working on first person environment features, I wanted to organize my co ...

Display a window.confirm alert to prevent any unauthorized changes to the URL

Looking to implement a feature that prevents changing the URL from my component. The goal is to display a message and allow users to choose between canceling (no change to URL) or confirming (change the URL). How can I achieve this in the upcoming 13.4 ver ...

Utilizing React Hook to fetch initial data in useEffect

Encountered a roadblock while attempting to update a hook when the web socket is triggered with new data. I noticed that the hooks are returning the default values I initialized them with inside my useEffect, whereas during rendering it shows the correct v ...

Google's Chart Tools are showcased within a scrollable <Div> element, allowing for easy navigation and

I have been utilizing the table feature within Google Chart Tools to showcase data retrieved from a database. Upon displaying this grid inside a scrollable div, I noticed that the column headings are wrapping to multiple lines instead of displaying in a si ...

Navigate to a new page seamlessly without the need for refreshing the current page in Django

Is there a way to navigate to another page without refreshing after clicking on a link to a different URL? For example, if I have a list of books displayed and I click on one of the books, can it redirect me to the selected book page without reloading the ...

Removing the white background in a modal form with rounded corners in Angular

Is there a way to remove the white background when opening a modal form with round corners? https://i.sstatic.net/cpr2h.png This is the CSS code for the modal form: .modal-msg { .modal-dialog { width: 25vw !important; max-width: 35vw !important ...

AngularJS, perform an action on an HTML element within an ng-repeat directive once the user interface has been rendered

When using AngularJS to add a row to an HTML table by clicking an "Add row" button and using $scope.photos.push({}), you may encounter an issue when trying to trigger the file dialog of the new row's file input field. Is it possible to do this and if ...

The Node.js POST request is unexpectedly returning 'undefined'

I'm currently working on a project for an online course and I'm encountering an issue with making an API call. It seems that I am getting undefined responses to my post request because the user input is not being retrieved properly. Below are the ...

How can I make an HTML textbox in C# MVC flash a background color for one second using TextBoxFor?

I'm currently working with MVC and have the following code in my razor view. @Html.TextBoxFor(x => model.DateTimeStamp, new { @readonly = "readonly", @class=ViewBag.IsLatest ? "latest":""}) I want to briefly change the background color of this te ...

Issues related to ng-model within a dropdown list

Currently, I am facing an issue with retrieving the selected value from a select element using ng-model. Even though the value is displayed correctly on the application page, it remains at the initial value in the app controller. Despite my efforts to find ...

Just easy highlighting using tags in Javascript

I have come across a code snippet that seems to be functioning well: <html> <head> <title>Testing JS Highlighting</title> <script type="text/javascript"> function highlight() { var t = ...

Enhance translation functionality within the loader module of ngx-translate

I have been tasked with building a unique Angular ngx-translate Loader that retrieves translations from either a cache or an API. I am currently facing a challenge in Case 2 section of the code: Here is the desired process: Reterieve translations from c ...

Troubleshooting problem with static divs not scrolling properly within an iframe on iOS devices

I'm encountering an issue with iOS scrolling on my iPad. All other platforms I've tested work perfectly fine except for this one. While I have a love-hate relationship with iOS, I managed to make the entire iframe content scroll within the parent ...

Using ASP.NET to cleverly include script files based on certain conditions

We are currently developing a CMS project in ASP.NET, utilizing jQuery for our client-side scripting needs. At the moment, our project includes the jquery-1.2.6.js file as a mandatory script file. Additional script files are loaded as needed, based on the ...

Using JavaScript, learn how to dynamically add a `<select class="form-control">` in Bootstrap

Currently delving into the realms of Bootstrap and jQuery, these are both new territories for me. I am looking to use JavaScript to dynamically create the <select class="form-control"> tag. In my JavaScript code snippet, I have this: var response ...

What is the process for converting to the optimal prefix with js-quantities?

I am working with a value of `1200000 mm' and I want to find a method that can automatically convert it to the best prefix. For example: import Qty from 'js-quantities' const qty = new Qty(1200000, 'mm').toBest() // will be conve ...

jQuery element with listener not triggering as expected

I'm facing some confusion while working on this issue. I am attempting to create a dynamic form where users can add descriptions, checkboxes, and number inputs as they please. Currently, I have developed a simple dynamic form using jQuery, which allow ...

Sometimes in a Durandal viewmodel, the view is not necessarily attached when the viewAttached() function is called

I implemented a ViewModel in Durandal with a function called viewAttached(). As mentioned in the documentation, this function is supposed to be executed after the view is attached to the DOM. Within my function, there is a jQuery selector targeting an el ...

Issue with Firefox browser: Arrow keys do not function properly for input tags in Twitter Bootstrap drop down menu

Requirement I need to include a login form inside the dropdown menu with username and password fields. Everything is working fine, except for one issue: Issue When typing, I am unable to use the arrow keys (up/down) in Firefox. This functionality works ...