-1

I have an html as below :

<div class="parent"> 
  <a href="#">parent</a>
  <div class="child">child</div>
</div>

Here is my Jquery binding an click event :

$(document).ready(function(){
    $('.parent').on('click', function(){
        alert('parent div');
    });

    $('.child').on('click', function(){
        alert('child div');
    });
});

I want, if I click on ".child" div, then event bound on "div.parent" should not be called. Again if I click on ".parent" div and its bound event should be executed.

I have tried with following -

 $('.child').on('click', function(){
        alert('child div');
        $(this).parents('.parent').off('click');
        return false;
    });

But above code has unbound the "click" event on parent div permanently, and again its click event does not trigger.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Gautam Rai
  • 2,445
  • 2
  • 21
  • 31
  • 1
    please see https://stackoverflow.com/questions/2364629/jquery-stop-child-triggering-parent-event/2364639 It has already been answered. – Dhiraj Aug 28 '17 at 18:25

4 Answers4

3

why don't you stop propagating to prevent bubbling up the event from child to achieve this

$(document).ready(function(){
    $('.parent').on('click', function(){
        alert('parent div');
    });

    $('.child').on('click', function(ev){
        ev.stopPropagation();
        alert('child div');
    });
});
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
1

Once you click on the child div, the event bubbles up through the DOM tree, which causes the parent div to be triggered, now to prevent this you need to stop propagation using event.stopPropagation() read more on it here

 $('.child').on('click', function(event){
    event.stopPropagation();
    alert('child div');
    return false;
});
Kingsley Solomon
  • 481
  • 1
  • 4
  • 9
1

Gautam,

Interesting problem. I was thinking subclasses. But since you have the div elements wrapped in each other, this is not feasible. Therefore, I recommend the following functions that will toggle the response until the next time it is clicked.

makeParentReady = function() {
    $('.parent').on('click', function(){
    myClass=this.className;
    alert('parent div');

    });

}

$(document).ready(function(){
        makeParentReady();

        $('.child').on('click', function(){
            myClass=this.className;
            alert('child div');
            $(this).parents('.parent').off('click');
            makeParentReady();
            return false;
            });
        });
0

Yes. Dhiraj is right. use topPropagation() function

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
    $('.parent').on('click', function(){
        alert('parent div');
        $('.child').click();
    });

    $('.child').on('click', function(e){
        e.stopPropagation()
        alert('child div');
    });
});

</script>

</head>
<body>

<div class="parent"> 
  <a href="#">parent</a>
  <div class="child">child</div>
</div>

</body>
</html>
volkinc
  • 2,143
  • 1
  • 15
  • 19