Limiting the click event to the parent div only

I've encountered a problem with my overlay setup. I have an overlay with a child div inside, and I want the overlay to close only when the user clicks on the overlay itself, not the child div. Even though I've implemented the closing function, it still closes when the user clicks on the child div. How can I resolve this issue?

<div id="overlay" onClick={this.props.hideOverlay}>

                <div className="ques_preview_div">

                </div>
</div>

The goal is for the overlay to remain open if the user clicks anywhere within the child div.

<div className="ques_preview_div">

</div>

Answer №1

I attempted to resolve the issue with my initial answer, but it was unsuccessful. As a result, I decided to create a CodePen demo to demonstrate the solution.

function childClick(event){
  event.stopPropagation()
  console.log('child')
}

The root of the problem lies in the event bubbling behavior of the child element. Even if the child element does not have an onClick function, a click event will still bubble up to its parent element and trigger its onClick function. To prevent this, we need to add an onClick listener to the child element and use stopPropagation() method to stop the event from propagating further.

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

Need to conceal certain images in Isotope, but ensure they can easily be revealed when necessary?

I have a collection of various images on my website coded with isotope. My question is, how can I hide specific images that I don't want to display initially, but have them appear when needed? I have managed to create a "show all" list using the code ...

Shifting a div element around the webpage and letting it fall into place if it intersects with another div using plain JavaScript

Check out this jsFiddle link. Upon opening it, you will encounter a moveable div. However, I am looking to enhance this functionality by allowing the div to disappear if moved to the 'trash' area. Essentially, when you place the moveable div in t ...

What's the best way to vertically center a div with a 100% width and a set padding-bottom on the screen?

I am struggling with positioning the following div on my webpage. .div { width:100%; padding-bottom:20%; background-color:blue; } <div class="div"></div> I have been searching for a solution to vertically center this responsive div on my web ...

Run Python scripts on imported files

I am facing an issue with my django application where I need to upload 2 files and run a python script (proto2.py) on them. In the HTML code, I included a button to trigger the file execution but it did not work: <div>Relay 1: <form action=" ...

Tips on confirming the completion of my form when the next button is pressed

I am working on a multi-step form with a jQuery script to split the forms. The first form has only one button - the next button, which takes the client to the next form. However, when I click on the next button in the first form, it moves to the next form ...

Ways to selectively expand a single collapse panel in ant-design

Currently, I am utilizing Ant-design collapse as an accordion. The specific functionality I am seeking is to have the first panel open by default when the page loads. Additionally, I want only the clicked panel to expand, with any previously open panels cl ...

Ways to ensure a function is only triggered once using onmouseover

I'm fairly new to JavaScript and I've been attempting to create a function that only runs once. Here's the logo I've been trying to animate: <img src="img/logot2.png" id="logoutjs" onmouseover="move()" ...

dividing an HTML string into individual variables using JavaScript

How can a string be split in pure JavaScript (without using JQuery/dojo/etc) in the most efficient and straightforward way? For example: var tempString = '<span id="35287845" class="smallIcon" title="time clock" style="color:blue;font-size:14px;" ...

There is a lag in the loading of css stylesheets

We created a single-page company website that utilizes three different stylesheets connected to the index.html. By clicking a button, users can switch between these stylesheets resulting in changes to colors, images, and text colors. However, Issue 1: The ...

IE6 disrupts stored layouts

I've encountered a strange issue with my design in IE6. It loads perfectly at first, but after reloading it a couple of times, everything suddenly collapses. The strange part is that when I upload an updated version, it fixes the issue, only to break ...

Unlocking the full potential of Konva Node in your ReactJs projects

I'm looking to implement node nesting in konva.js within my react app. Any guidance on how to incorporate this functionality would be greatly appreciated. Thanks in advance! ...

Checking for the Existence of a Class Element within a String using JavaScript

Within my web project, there is a scenario where if a user has been logged out in one browser tab, they will automatically be redirected to the login page in any other browser tab after interacting with that page (such as clicking on a link). However, this ...

The Bootstrap 4 column is not floating properly to the right as it is leaving a gap on the

My right sidebar is not floating to the right and has space from the right side. You can view it at this link: <div class="col-md-2 col-lg-2 col-sm-2" id="right-sidebar" style="background-color:orange; float: right; right: 0!important; width: 100%;"& ...

Retrieving the output from a nested scope within a function

I have a scenario where I am working with a function that has a nested "then" function containing a return statement. My goal is to combine this inner return data with the outer return data. Below is the code snippet: public async getAllWidgets(): Promis ...

What could be the reason for the unexpected behavior of position sticky?

I am encountering an issue while trying to figure out why the container__item--special element behaves as a sticky element in this code snippet <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> ...

NextJS router.push() function is malfunctioning and not behaving as intended

Currently, I am encountering issues with redirecting the user to the search results using router.push(). Strangely enough, when I modify the URL to a non-existent page, the route changes successfully. However, nothing happens when I attempt to use the URL ...

Attempting to include two runat=server forms within a single ASP.NET web form

As part of a course project in Web forms asp.net, I have created a master page for my website. One feature I am trying to implement is a search text box that needs to be included in the following code: <form runat="server"> This is the master code: ...

What is the reason behind the overflow in the HTML body?

I am currently honing my skills in front-end development by attempting to replicate the functionality of . Everything seems to be working fine, except for the fact that there is an overflow issue identified within the <body> element when inspecting w ...

Utilizing DRACOLoader alongside GLTFLoader within ReactJs

I have encountered a scenario where I am attempting to load a GLTF file that has been compressed using DRACO Compression. While I have successfully executed it using plain javascript, I am encountering challenges when trying to integrate the loader with Re ...

Having trouble with the functionality of my JavaScript slider

After attempting to incorporate a slider into my website, I encountered an issue where the slider was not functioning as expected. The slider I tried to implement can be found here: https://codepen.io/amitasaurus/pen/OMbmPO The index of the site can be a ...