-4

See my snippet, as I click the parent element, the child element is shown but as I click the child element, the parent element events seems also triggered along with the child elements event, any ideas to trigger only parent elements event when on click and only child elements event when on click (must not bubble the parent elements event)? any ideas, help, clues, suggestions, recommendations please?

$(document).ready(function(){
  $("#child").hide();
  $("#parent").click(function(){
    alert("parent event is triggered");
    $("#child").show();
  });
  $("#child").click(function(){
    alert("child event is triggered");
  });
});
#parent{
 padding: 15px;
 border: 3px dotted #cccccc;
}
#child{
 padding: 4px;
 border: 1px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="parent">
  This is parent element, click to show the child element
  <div id="child">Child</div>
</div>
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

2 Answers2

2
$("#child").click(function(){
    alert("child event is triggered");
    event.stopPropagation();
  });
Avinash
  • 812
  • 8
  • 17
0

Have you tried adding a stopPropogation

$("#child").click(function(event){
    alert("child event is triggered");
    event.stopPropagation();
});

Reference

Rino Raj
  • 6,264
  • 2
  • 27
  • 42
Obsidian
  • 515
  • 3
  • 10