Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Event bubbling for a disabled element

I have a button that has a style

pointer-events: none;

And this button has a parent element that performs a collapsible event. I don't know how to prevent this button from triggering its parent elements collapsible event. This is caused because of the button style which is pointer-events: none

Thanks

like image 944
rajkumarts Avatar asked Sep 24 '13 15:09

rajkumarts


People also ask

What does event DOT stopPropagation do?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.

Which method prevent the event from bubbling up the DOM tree?

stopPropagation()Returns: undefined. Description: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

Does prevent default stop bubbling?

preventDefault() prevents the browsers default behaviour, but does not stop the event from bubbling up the DOM. The event. stopPropagation() prevents the event from bubbling up the DOM, but does not stop the browsers default behaviour.


1 Answers

Assuming the following html:

<div class="collapsible">
    <button>Hi</button>
</div>

You could do something like this:

$('.collapsible').click(function(e) {
    if ($(this).children('button').css('pointer-events') == 'none') return;

    //do collapse
});

or maybe this:

$('.collapsible').click(function(e) {        
    //do collapse
});

$('.collapsible button').click(function(e) {
   if ($(this).css('pointer-events') == 'none')
       e.stopPropagation();
});
like image 116
Jason P Avatar answered Oct 18 '22 16:10

Jason P