How do I prevent a parent's onclick event from firing when a child anchor is clicked?

I’m currently using jQuery to make a div clickable and in this div I also have anchors. The problem I’m running into is that when I click on an anchor both click events are firing (for the div and the anchor). How do I prevent the div’s onclick event from firing when an anchor is clicked?

Here’s the broken code:

JavaScript

var url = $("#clickable a").attr("href");

$("#clickable").click(function() {
    window.location = url;
    return true;
})

HTML

<div id="clickable">
    <!-- Other content. -->
    <a href="http://foo.com">I don't want #clickable to handle this click event.</a>
</div>

28 thoughts on “How do I prevent a parent's onclick event from firing when a child anchor is clicked?”

  1. Events bubble to the highest point in the DOM at which a click event has been attached. So in your example, even if you didn’t have any other explicitly clickable elements in the div, every child element of the div would bubble their click event up the DOM to until the DIV’s click event handler catches it.

    There are two solutions to this is to check to see who actually originated the event. jQuery passes an eventargs object along with the event:

    $("#clickable").click(function(e) {
        var senderElement = e.target;
        // Check if sender is the <div> element e.g.
        // if($(e.target).is("div")) {
        window.location = url;
        return true;
    });
    

    You can also attach a click event handler to your links which tell them to stop event bubbling after their own handler executes:

    $("#clickable a").click(function(e) {
       // Do something
       e.stopPropagation();
    });
    
    Reply
  2. Use stopPropagation method, see an example:

    $("#clickable a").click(function(e) {
       e.stopPropagation();
    });
    

    As said by jQuery Docs:

    stopPropagation method prevents the event from bubbling up the DOM
    tree, preventing any parent handlers from being notified of the event.

    Keep in mind that it does not prevent others listeners to handle this event(ex. more than one click handler for a button), if it is not the desired effect, you must use stopImmediatePropagation instead.

    Reply
  3. add a as follows:

    <a href="http://foo.com" onclick="return false;">....</a>
    

    or return false; from click handler for #clickable like:

      $("#clickable").click(function() {
            var url = $("#clickable a").attr("href");
            window.location = url;
            return false;
       });
    
    Reply
  4. All solution are complicated and of jscript. Here is the simplest version:

    var IsChildWindow=false;
    
    function ParentClick()
    {
        if(IsChildWindow==true)
        {
            IsChildWindow==false;
            return;
        }
        //do ur work here   
    }
    
    
    function ChildClick()
    {
        IsChildWindow=true;
        //Do ur work here    
    }
    
    Reply
  5. To specify some sub element as unclickable write the css hierarchy as in the example below.

    In this example I stop propagation to any elements (*) inside td inside tr inside a table with the class “.subtable”

    $(document).ready(function()
    {    
       $(".subtable tr td *").click(function (event)
       {
           event.stopPropagation();
       });
    
    });
    
    Reply
  6. you can also try this

    $("#clickable").click(function(event) {
       var senderElementName = event.target.tagName.toLowerCase();
       if(senderElementName === 'div')
       {
           // do something here 
       } 
       else
       {
          //do something with <a> tag
       }
    });
    
    Reply
  7. If you have multiple elements in the clickable div, you should do this:

    $('#clickable *').click(function(e){ e.stopPropagation(); });
    
    Reply
  8. var inner = document.querySelector("#inner");
    var outer = document.querySelector("#outer");
    inner.addEventListener('click',innerFunction);
    outer.addEventListener('click',outerFunction);
    
    function innerFunction(event){
      event.stopPropagation();
      console.log("Inner Functiuon");
    }
    
    function outerFunction(event){
      console.log("Outer Functiuon");
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>Pramod Kharade-Event with Outer and Inner Progration</title>
    </head>
    <body>
    <div id="outer" style="width:100px;height:100px;background-color:green;">
      <div id="inner" style="width:35px;height:35px;background-color:yellow;"></div>
    </div>
    </body>
    </html>
    Reply
  9. Here my solution for everyone out there looking for a non-jQuery code (pure javascript)

    document.getElementById("clickable").addEventListener("click", function( e ){
        e = window.event || e; 
        if(this === e.target) {
            // put your code here
        }
    });
    

    Your code wont be executed if clicked on parent’s childs

    Reply
  10. Here’s an example using Angular 2+

    For example, if you wanted to close a Modal Component if the user clicks outside of it:

    // Close the modal if the document is clicked.
    
    @HostListener('document:click', ['$event'])
    public onDocumentClick(event: MouseEvent): void {
      this.closeModal();
    }
    
    // Don't close the modal if the modal itself is clicked.
    
    @HostListener('click', ['$event'])
    public onClick(event: MouseEvent): void {
      event.stopPropagation();
    }
    
    Reply
  11. If you do not intend to interact with the inner element/s in any case, then a CSS solution might be useful for you.

    Just set the inner element/s to pointer-events: none

    in your case:

    .clickable > a {
        pointer-events: none;
    }
    

    or to target all inner elements generally:

    .clickable * {
        pointer-events: none;
    }
    

    This easy hack saved me a lot of time while developing with ReactJS

    Browser support could be found here: http://caniuse.com/#feat=pointer-events

    Reply
  12. ignoreParent() is a pure JavaScript solution.

    It works as an intermediary layer that compares the coordinates of the mouse click with the coordinates of the child element/s. Two simple implementation steps:

    1. Put the ignoreParent() code on your page.

    2. Instead of the parent’s original onclick=”parentEvent();”, write:

    onclick="ignoreParent(['parentEvent()', 'child-ID']);"
    

    You may pass IDs of any number of child elements to the function, and exclude others.

    If you clicked on one of the child elements, the parent event doesn’t fire. If you clicked on parent, but not on any of the child elements [provided as arguments], the parent event is fired.

    ignoreParent() code on Github

    Reply
  13. e.stopPropagation() is a correct solution, but in case you don’t want to attach any event handler to your inner anchor, you can simply attach this handler to your outer div:

    e => { e.target === e.currentTarget && window.location = URL; }
    
    Reply
  14. Here is a non jQuery solution that worked for me.

    <div style="background:cyan; width:100px; height:100px;" onclick="if (event.srcElement==this) {console.log('outer');}">
        <a style="background:red" onclick="console.log('inner');">Click me</a>
    </div>
    Reply
  15. You can check whether the target is not your div-element and then issue another click event on the parent after which you will "return" from the handle.

    $('clickable').click(function (event) {
        let div = $(event.target);
        if (! div.is('div')) {
           div.parent().click();
           return;
        }
        // Then Implement your logic here
    }
    
    Reply
  16. If a child element is clicked, then the event bubbles up to the parent and event.target !== event.currentTarget.

    So in your function, you can check this and return early, i.e.:

    var url = $("#clickable a").attr("href");
    $("#clickable").click(function(event) {
        if ( event.target !== event.currentTarget ){
            // user clicked on a child and we ignore that
            return;
        }
        window.location = url;
        return true;
    })
    
    Reply
  17. I dont have the reputation to comment but this answer by Gleno was simple and worked perfectly for me, it deserves far more up votes

    onclick="functionCall();event.stopPropagation();

    Reply
  18. In case someone had this issue using React, this is how I solved it.

    scss:

    #loginBackdrop {
    position: absolute;
    width: 100% !important;
    height: 100% !important;
    top:0px;
    left:0px;
    z-index: 9; }
    
    #loginFrame {
    width: $iFrameWidth;
    height: $iFrameHeight;
    background-color: $mainColor;
    position: fixed;
    z-index: 10;
    top: 50%;
    left: 50%;
    margin-top: calc(-1 * #{$iFrameHeight} / 2);
    margin-left: calc(-1 * #{$iFrameWidth} / 2);
    border: solid 1px grey;
    border-radius: 20px;
    box-shadow: 0px 0px 90px #545454; }
    

    Component’s render():

    render() {
        ...
        return (
            <div id='loginBackdrop' onClick={this.props.closeLogin}>
                <div id='loginFrame' onClick={(e)=>{e.preventDefault();e.stopPropagation()}}>
                 ... [modal content] ...
                </div>
            </div>
        )
    }
    

    By a adding an onClick function for the child modal (content div) mouse click events are prevented to reach the ‘closeLogin’ function of the parent element.

    This did the trick for me and I was able to create a modal effect with 2 simple divs.

    Reply
  19. Inline Alternative:

    <div>
        <!-- Other content. -->
        <a onclick='event.stopPropagation();' href="http://foo.com">I don't want #clickable to handle this click event.</a>
    </div>
    
    Reply

Leave a Comment