1

say, I have

HTML:
<div onclick="parent()">
    <div onclick="child()"></div>
</div>

SCRIPT: {
function parent() {
    console.log("I'm your father");
}

function child() {
    console.log("You're not my father");
}

In this situation, if I click child element, it's parent element is clicked also, so parent() child() both are called. How can I call child() only without it's parent being clicked?

kispi
  • 185
  • 1
  • 12
  • `Event.stopPropagation()` (also, better to assign handlers in Javascript rather than HTML attributes - that's as bad as `eval`) – CertainPerformance May 08 '18 at 07:04
  • @CertainPerformance Do you mean I cannot use onclick, ng-click... for binding function do DOM? Should I use bunch of JQueries? – kispi May 08 '18 at 07:19
  • it could be right for performance-wise but, isn't that easy to maintain rather than bunch of jquery selectors? – kispi May 08 '18 at 07:21
  • @kispi No where did you mention `ng-click` in your question, but inline `onclick` is bad. – Asons May 08 '18 at 07:22
  • @kispi `ng-click` is different from the `onclick` attribute. The problem isn't performance - the problem is code readability and maintainability. Inline event handlers are essentially `eval` inside HTML markup - they're bad practice and result in poorly factored, hard-to-manage code. Attach the handlers with Javascript instead. (no, there's absolutely no need for jQuery just to assign a handler) Separate presentation from content: keep content in your HTML and your Javascript in your Javascript. – CertainPerformance May 08 '18 at 07:23
  • @CertainPerformance thanks, but how about ng-click? Can we think it seperates presentation and content? Or is it not a problem since it's 'ng' and everyone knows that there will be everything inside of component? – kispi May 08 '18 at 07:29

4 Answers4

3

function parent() {
    console.log("I'm your father");
}

function child() {
    console.log("You're not my father");
    window.event.cancelBubble = "true";
}
<div onclick="parent()" style="height : 40px;width:40px; background:blue">
    <div onclick="child()" style="height : 10px;width:10px; background:red"></div>
</div>

You can use

window.event.cancelBubble = "true"
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
1

Add the following in your child click event to stop event bubbling.

 event.stopPropagation();
Mohit Agarwal
  • 201
  • 2
  • 10
1

Try event.stopPropagation() from child() which will prevent further propagation of the current event in the capturing and bubbling phases.

function parent() {
    console.log("I'm your father");
}

function child(e) {
    e.stopPropagation();
    console.log("You're not my father");
}
<div onclick="parent()">Parent
    <div onclick="child(event)">Child</div>
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

That's called bubbling. The process is called “bubbling”, because events bubble from the inner element up through parents like a bubble in the water.

When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

You can use stopPropagation method.

function parent() {
    console.log("I'm your father");
}

function child(event) {
    event.stopPropagation();
    console.log("You're not my father");
}
#parent{
  background-color:red;
  width:100px;
  height:100px;
}

#child {
  background-color:blue; 
  width:30px;
  align:center;
  height:30px;
}
<div id="parent" onclick="parent()">
    <div id="child" onclick="child(event)"></div>
</div>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128