onClick event of child component is not getting triggered for stacked parent and child component












0















I have the following two components with thier respective onClick() handlers. Problem is doSomethingElse is never getting and directly doSomething is getting called.



<ParentComponent onClick={this.doSomething}>
<ChildComponent onClick={this.doSomethingElse} />
</Parent>


Child is shown as like proover on parent using z-index.



class Parent extends React.Component {
constructor(props) {
super(props);

}

handleP = (e) => {
alert("parent")
};

render() {
return (

<div onClick={this.handleP} style={{
left: 100,
top: 100,
zIndex: 100,
position: 'absolute',
}}>
Parent Text
<Child />
</div>

)
}
}

class Child extends React.Component {
constructor(props) {
super(props);

}

handleC = (e) => {
alert("child")
};

render() {
return (

<div onClick={this.handleC} style={{
left: 1,
top: 1,
zIndex: 1000,
position: 'absolute',
}}>
Child Text
</div>

)
}
}



React.render(
<Parent />,
document.getElementById('react_example')
);


Jsbin










share|improve this question

























  • can you create a working example and show the actual full code?

    – Mayank Shukla
    Nov 23 '18 at 6:27











  • It would be useful if we know what Parent and Child Components are!

    – Lucifer
    Nov 23 '18 at 6:54











  • @MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

    – Sandip Pingle
    Nov 23 '18 at 7:20













  • try to use the same structure as you are using in your actual code.

    – Mayank Shukla
    Nov 23 '18 at 7:33











  • It seems to be working fine.

    – Tholle
    Nov 23 '18 at 8:43
















0















I have the following two components with thier respective onClick() handlers. Problem is doSomethingElse is never getting and directly doSomething is getting called.



<ParentComponent onClick={this.doSomething}>
<ChildComponent onClick={this.doSomethingElse} />
</Parent>


Child is shown as like proover on parent using z-index.



class Parent extends React.Component {
constructor(props) {
super(props);

}

handleP = (e) => {
alert("parent")
};

render() {
return (

<div onClick={this.handleP} style={{
left: 100,
top: 100,
zIndex: 100,
position: 'absolute',
}}>
Parent Text
<Child />
</div>

)
}
}

class Child extends React.Component {
constructor(props) {
super(props);

}

handleC = (e) => {
alert("child")
};

render() {
return (

<div onClick={this.handleC} style={{
left: 1,
top: 1,
zIndex: 1000,
position: 'absolute',
}}>
Child Text
</div>

)
}
}



React.render(
<Parent />,
document.getElementById('react_example')
);


Jsbin










share|improve this question

























  • can you create a working example and show the actual full code?

    – Mayank Shukla
    Nov 23 '18 at 6:27











  • It would be useful if we know what Parent and Child Components are!

    – Lucifer
    Nov 23 '18 at 6:54











  • @MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

    – Sandip Pingle
    Nov 23 '18 at 7:20













  • try to use the same structure as you are using in your actual code.

    – Mayank Shukla
    Nov 23 '18 at 7:33











  • It seems to be working fine.

    – Tholle
    Nov 23 '18 at 8:43














0












0








0


0






I have the following two components with thier respective onClick() handlers. Problem is doSomethingElse is never getting and directly doSomething is getting called.



<ParentComponent onClick={this.doSomething}>
<ChildComponent onClick={this.doSomethingElse} />
</Parent>


Child is shown as like proover on parent using z-index.



class Parent extends React.Component {
constructor(props) {
super(props);

}

handleP = (e) => {
alert("parent")
};

render() {
return (

<div onClick={this.handleP} style={{
left: 100,
top: 100,
zIndex: 100,
position: 'absolute',
}}>
Parent Text
<Child />
</div>

)
}
}

class Child extends React.Component {
constructor(props) {
super(props);

}

handleC = (e) => {
alert("child")
};

render() {
return (

<div onClick={this.handleC} style={{
left: 1,
top: 1,
zIndex: 1000,
position: 'absolute',
}}>
Child Text
</div>

)
}
}



React.render(
<Parent />,
document.getElementById('react_example')
);


Jsbin










share|improve this question
















I have the following two components with thier respective onClick() handlers. Problem is doSomethingElse is never getting and directly doSomething is getting called.



<ParentComponent onClick={this.doSomething}>
<ChildComponent onClick={this.doSomethingElse} />
</Parent>


Child is shown as like proover on parent using z-index.



class Parent extends React.Component {
constructor(props) {
super(props);

}

handleP = (e) => {
alert("parent")
};

render() {
return (

<div onClick={this.handleP} style={{
left: 100,
top: 100,
zIndex: 100,
position: 'absolute',
}}>
Parent Text
<Child />
</div>

)
}
}

class Child extends React.Component {
constructor(props) {
super(props);

}

handleC = (e) => {
alert("child")
};

render() {
return (

<div onClick={this.handleC} style={{
left: 1,
top: 1,
zIndex: 1000,
position: 'absolute',
}}>
Child Text
</div>

)
}
}



React.render(
<Parent />,
document.getElementById('react_example')
);


Jsbin







javascript reactjs mouseevent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:19







Sandip Pingle

















asked Nov 23 '18 at 6:25









Sandip PingleSandip Pingle

3551732




3551732













  • can you create a working example and show the actual full code?

    – Mayank Shukla
    Nov 23 '18 at 6:27











  • It would be useful if we know what Parent and Child Components are!

    – Lucifer
    Nov 23 '18 at 6:54











  • @MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

    – Sandip Pingle
    Nov 23 '18 at 7:20













  • try to use the same structure as you are using in your actual code.

    – Mayank Shukla
    Nov 23 '18 at 7:33











  • It seems to be working fine.

    – Tholle
    Nov 23 '18 at 8:43



















  • can you create a working example and show the actual full code?

    – Mayank Shukla
    Nov 23 '18 at 6:27











  • It would be useful if we know what Parent and Child Components are!

    – Lucifer
    Nov 23 '18 at 6:54











  • @MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

    – Sandip Pingle
    Nov 23 '18 at 7:20













  • try to use the same structure as you are using in your actual code.

    – Mayank Shukla
    Nov 23 '18 at 7:33











  • It seems to be working fine.

    – Tholle
    Nov 23 '18 at 8:43

















can you create a working example and show the actual full code?

– Mayank Shukla
Nov 23 '18 at 6:27





can you create a working example and show the actual full code?

– Mayank Shukla
Nov 23 '18 at 6:27













It would be useful if we know what Parent and Child Components are!

– Lucifer
Nov 23 '18 at 6:54





It would be useful if we know what Parent and Child Components are!

– Lucifer
Nov 23 '18 at 6:54













@MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

– Sandip Pingle
Nov 23 '18 at 7:20







@MayankShukla added sample code please check edited question. But in this example code is working but in my case its not working.

– Sandip Pingle
Nov 23 '18 at 7:20















try to use the same structure as you are using in your actual code.

– Mayank Shukla
Nov 23 '18 at 7:33





try to use the same structure as you are using in your actual code.

– Mayank Shukla
Nov 23 '18 at 7:33













It seems to be working fine.

– Tholle
Nov 23 '18 at 8:43





It seems to be working fine.

– Tholle
Nov 23 '18 at 8:43












1 Answer
1






active

oldest

votes


















0














@SandipPingle the issue is because, by default a click event bubbles till window. So after the child handler is invoked the event bubbles to parent component. For that you need to stop the event propogation. Refer to this question and this sandbox for a sample fixed version based on this answer :)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441549%2fonclick-event-of-child-component-is-not-getting-triggered-for-stacked-parent-and%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    @SandipPingle the issue is because, by default a click event bubbles till window. So after the child handler is invoked the event bubbles to parent component. For that you need to stop the event propogation. Refer to this question and this sandbox for a sample fixed version based on this answer :)






    share|improve this answer




























      0














      @SandipPingle the issue is because, by default a click event bubbles till window. So after the child handler is invoked the event bubbles to parent component. For that you need to stop the event propogation. Refer to this question and this sandbox for a sample fixed version based on this answer :)






      share|improve this answer


























        0












        0








        0







        @SandipPingle the issue is because, by default a click event bubbles till window. So after the child handler is invoked the event bubbles to parent component. For that you need to stop the event propogation. Refer to this question and this sandbox for a sample fixed version based on this answer :)






        share|improve this answer













        @SandipPingle the issue is because, by default a click event bubbles till window. So after the child handler is invoked the event bubbles to parent component. For that you need to stop the event propogation. Refer to this question and this sandbox for a sample fixed version based on this answer :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 15:58









        klvenkyklvenky

        129211




        129211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441549%2fonclick-event-of-child-component-is-not-getting-triggered-for-stacked-parent-and%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'