Hawk
Hawk

Reputation: 783

How to use stopPropagation() using Javascript in a function with parameters

I have this function that takes parameters, I do not get how to stop the bubbling phase of the event when my function has parameters. I have tried the following code and none of them work.

function paraHandler(msg, e) {
    alert(msg);
    e.stopPropagation();
}

function paraHandler(msg) {
    alert(msg);
    msg.stopPropagation();
}

function paraHandler(msg) {
    alert(msg);
    this.stopPropagation();
}

var para = document.getElementById('closer');
para.addEventListener('click', function () {
    paraHandler('hello');
}, false);
window.addEventListener('click', function () {
    alert('window clicked');
}, false);

I get this error message: Uncaught TypeError: undefined is not a function. The event shouldn't bubble up and alert 'window clicked' after para has been clicked.

Upvotes: 0

Views: 482

Answers (1)

Moak
Moak

Reputation: 12885

You want to pass the event from the callback to the handler

para.addEventListener('click', function(event){
     paraHandler('hello',event);}
,false); 

Upvotes: 1

Related Questions