Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ignore click if focus is on button in element

There is a problem, I have a clickable element and if I want to click on a button in the element, then both of them will work, when I need only the button to work. Implementation via ReactJS. Button position absolute in element! Code example:

 <div onClick={() => alert(1)}>
    ...content
    <button onClick={() => alert(2)}>Click me!</button>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you have a clickable element inside a clickable element, then you need to use event.stopPropagation();

So here

const myMethod = event => {
  alert(2);
  event.stopPropagation();
}

 <div onClick={() => alert(1)}>
    ...content
    <button onClick={myMethod}>Click me!</button>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading