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

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

reasons to stop father seeing child
can a parent keep a child from the other parent without a court order
mother will not allow father to see child
keeping a child away from the other parent can backfire
withholding a child from another parent
tennessee parental kidnapping laws
prevent parent click event react
can a father kidnap his child

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>

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();
});

How do I prevent a parent's onclick event from firing when a child , 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  Avoid using yelling as a communication strategy. If your parents are yelling at you, don't yell back. Stay calm and lead by calm example. Wait until they are done talking (unless you are getting too angry; then leave immediately).


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.

Can I Legally Prevent My Children from Seeing Their Other Parent?, Washington family law attorneys at McKinley Irvin discuss what your options are if you wish to prevent your ex from any contact with your  How Do I Prevent Child Sexual Abuse as a Parent? The purpose of this tool is to ask important questions when thinking about who has access to your children. After answering each question, you will be provided with immediate feedback and directed to resources to learn more.


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

13 Ways To Prevent Parental Kidnapping, The key in preventing parental kidnapping is to put protections and interventions in place that will prevent the child abduction from ever happening in the first  To ease the transition, consider putting a mattress on the floor in your kid’s room, and sleeping there for a few nights, suggests Briggs. You can slowly move the mattress further from the bed until you’re no longer in the room at all.


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
   }
});

Prevention Tips, For a brochure on preventing international parental child abduction, click English or Español. Tips to Prevent an Abduction from Occurring. Take  And no, it’s not birth control–though that obviously helps too, even if conservatives don’t believe so. Joan Walsh (boldface mine):. Interestingly, in this country there seems to be one almost foolproof way to prevent single parenthood (besides contraception): college.


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

Prevention, of being abducted by a parent, legal guardian, or someone acting on their behalf, here are steps you can take immediately to help prevent the abduction:. 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.


Top 3 Improper Ways Parents Stop a Child From Seeing the Other , Is the mother's basis to stop a child from seeing the other parent unjustified? Yes, it is. Through our extensive experience in child custody cases, we could probably​  Parents, children, and school administrators can take steps to prevent cyberbullying before it starts or becomes worse. Here are practical steps to stop cyberbullying.


What Prevents Shared Custody? | Blog, If one parent has a history of abuse or neglect that would potentially expose the child to physical or psychological harm, it might prevent that  Find Parental Control On Computer. Find Quick Info from Multiple Sources


Parenting Time Interference - Family Law, To prevent this, and to keep their children out of parental disagreements, some parents may include a provision in their custody agreements  Find out Parental control computer on Shopping.net. Find Parental control computer here