Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent parent click when click on child

I want to prevent parent click when I clicking on child SPAN. I tried

    e.stopPropagation

    and thi way

    if (!e) e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();

I have html like:

 <label for="parent_id"> Some text
   <span id="child_id">Click child</span>
 </label>
 <input id="parent_id" />

Function for Parent element

$('#parent_id').click(function (e) { SomeParentCode }

Function for Child element.

$('#child_id').click(function (e) {
      e.stopPropagation();
       But I what to prevent parent click
       SomeChildCode
}
like image 687
Mag Avatar asked Dec 06 '25 19:12

Mag


1 Answers

Use preventDefault and stopPropagation.

$('#child_id').on('click', function (e) {
            e.preventDefault();
            e.stopPropagation();
});
like image 82
JF it Avatar answered Dec 08 '25 07:12

JF it