Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how prevent the parent click event on change a checkbox

I have a checkbox inside a parent container which has a click event, whenever I try to click the checkbox parent click works first and following by the change event, I am using e.stopPropagation(); on both the parent and child events, but still, it's not working

// make the .parent react
 function grandParent(){
    alert('Grand Parent: You hit me, my child or my grand child, now deal with me!');
}

// make the .parent react
$('.parent').on('click', function(e){
    e.stopPropagation()
    alert('Parent : Don\'t you dare hitting me or my child, again!');
});

// make the child cry, when we hit him.
$('.child').on('click', function(e){
e.stopPropagation();
 alert('Child : waaaaaa waaaa waa huh huh waaa waaaa!');
});

$('.hello').on('change', function(e){
e.stopPropagation();
 alert('checkbox clicked');
});

Fiddle example

like image 980
Jothi Kannan Avatar asked Mar 08 '23 13:03

Jothi Kannan


2 Answers

You have to bind the click event on the checkbox and not the change event: http://jsfiddle.net/ohc8jt6w/

like image 169
rvandoni Avatar answered Mar 10 '23 04:03

rvandoni


Sequence of the event matters , where the click event occurs first and Change event the next ,So in your case you need to change the type of event handling to Click click here to see the sequence / priority of events happening after clicking on check box

$('.hello').on('click change', function(e){
e.stopPropagation(); 
 alert('checkbox '+e.type);
});
like image 42
ANR Upgraded Version Avatar answered Mar 10 '23 04:03

ANR Upgraded Version