Search code examples
javascriptevent-bubbling

Capture event in a function to stop it from bubling


Note: I now about event bubling and how to stop it.

Code ==>

function foo ( bar ){
//do something
}

In the above function,i want to capture the event so I can stop it from bubling out.

What i've tried so far -->

Try 1

function foo ( bar,event ){
//do something
}

Try 2

function foo (bar ){
if( window.event || event )
   // code to stop bubling

//do something
}

Not using jQuery, vanilla js solution required. Using this method for prevention of event bubling.

-- beginner.


Solution

  • Try:

    function foo ( bar,event ){
      if (event.stopPropagation) {
        event.stopPropagation();
      } else {
        event.cancelBubble = true;
      } 
    }