Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent event bubling doesn't work in jquery

I have the DOM like this

<div class="modal">
<li>

<div id="block1"></div>
<div id="block2">

<div class="btn-wrap">
<button>cancel</button>
<button>save</button>
</div>

</div>

</li>

I have a click event attached to li and when I click button of block2, that event occurred. How to prevent that to happened?

I tried using on() but it doesn't work.

$('.btn-wrap').on('click','button:first-child',function(e){
            e.preventDefault();
            alert('test');
        });
like image 251
Alice Xu Avatar asked Jul 06 '15 04:07

Alice Xu


1 Answers

You should use stopPropagation to prevent event bubbling:

$('.btn-wrap').on('click','button:first-child',function(e){
   e.stopPropagation();
   alert('test');
});
like image 73
Bhojendra Rauniyar Avatar answered Nov 02 '22 22:11

Bhojendra Rauniyar