Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append my Event handler before existing handler

Let's suppose there is an <img> element that has some onclick event handler. For example onclick it does alert("OldEventHandler").

I would like to add my event handler there, before the existing one. For example my event handler function does alert("NewEventHandler").

So on click I would like to see "NewEventHandler" popup, and then "OldEventHandler" popup.

This needs to be implemented in pure JavaScript; no jQuery Please.

like image 701
Scherbius.com Avatar asked Jun 02 '11 20:06

Scherbius.com


1 Answers

You can save the original handler, then call it after yours is done:

var oldHandler = myElement.onclick;

myElement.onclick = function() {
    // do your stuff here
    ...

    // then call the original
    oldHandler.apply(this, arguments);
}
like image 129
Alnitak Avatar answered Nov 07 '22 07:11

Alnitak