0

I have created a overlay and inside that overlay there's another div. I want to close the overlay when the user click on the overlay part and not do anything if the user clicks the child div. I have implemented the closing the overlay function.But the issue is it closes the overlay even if the user clicks on the child div. How can I fix this?

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

                <div className="ques_preview_div">

                </div>
</div>

So basically the overlay should not close if the user clicks somewhere in this div.

<div className="ques_preview_div">

</div>
CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182

1 Answers1

1

My previous answer did not work therefore I created a CodePen

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

the problem is based on event bubbling of the child. So if the child is clicked although it does not have a onClick function it still bubbles upwards -> the parent onClick is called. To prevent this we have to add a onClick for the child and simply stopPropagation()

d3orn
  • 188
  • 1
  • 8